Commit e1aa3933 authored by Ben Boeckel's avatar Ben Boeckel Committed by Ben Boeckel

warnings: fix strncpy warnings

GCC is getting smarter and detects that we're not copying the NUL
terminator over.

There are two approaches to fixing the warning:

  - where code is already a bit more complicated, just use `std::string`
    instead of C-style string manipulations; and
  - where we were doing `strncpy(dest, src, strlen(src))`, just use
    `strcpy` instead since the destination sizes are made based on the
    size of `src` anyways.
parent ba0addf8
......@@ -108,15 +108,18 @@ void vtkObjectFactory::LoadDynamicFactories()
#ifndef _WIN32_WCE
LoadPath = getenv("VTK_AUTOLOAD_PATH");
#endif
if(LoadPath == nullptr || strlen(LoadPath) == 0)
if(LoadPath == nullptr || LoadPath[0] == 0)
{
return;
}
char* CurrentPath = new char[strlen(LoadPath)+1];
std::string CurrentPath;
CurrentPath.reserve(strlen(LoadPath) + 1);
char* SeparatorPosition = LoadPath; // initialize to env variable
while(SeparatorPosition)
{
size_t PathLength =0;
CurrentPath.clear();
size_t PathLength = 0;
// find PathSeparator in LoadPath
SeparatorPosition = strchr(LoadPath, PathSeparator);
// if not found then use the whole string
......@@ -129,23 +132,19 @@ void vtkObjectFactory::LoadDynamicFactories()
PathLength = static_cast<size_t>(SeparatorPosition - LoadPath);
}
// copy the path out of LoadPath into CurrentPath
strncpy(CurrentPath, LoadPath, PathLength);
// add a null terminator
CurrentPath[PathLength] = 0;
CurrentPath.append(LoadPath, PathLength);
// Get ready for the next path
LoadPath = SeparatorPosition+1;
LoadPath = SeparatorPosition + 1;
// Load the libraries in the current path
vtkObjectFactory::LoadLibrariesInPath(CurrentPath);
}
// clean up memory
delete [] CurrentPath;
}
// A file scope helper function to concat path and file into
// a full path
static char* CreateFullPath(const char* path, const char* file)
static char* CreateFullPath(const std::string& path, const char* file)
{
size_t lenpath = strlen(path);
size_t lenpath = path.size();
char* ret = new char[lenpath + strlen(file)+2];
#ifdef _WIN32
const char sep = '\\';
......@@ -153,7 +152,7 @@ static char* CreateFullPath(const char* path, const char* file)
const char sep = '/';
#endif
// make sure the end of path is a separator
strcpy(ret, path);
strcpy(ret, path.c_str());
if(ret[lenpath-1] != sep)
{
ret[lenpath] = sep;
......@@ -190,7 +189,7 @@ inline int vtkNameIsSharedLibrary(const char* name)
return (ret != nullptr);
}
void vtkObjectFactory::LoadLibrariesInPath(const char* path)
void vtkObjectFactory::LoadLibrariesInPath(const std::string& path)
{
vtksys::Directory dir;
if(!dir.Load(path))
......
......@@ -263,7 +263,7 @@ private:
/**
* Load all dynamic libraries in the given path
*/
static void LoadLibrariesInPath( const char*);
static void LoadLibrariesInPath(const std::string&);
// list of registered factories
static vtkObjectFactoryCollection* RegisteredFactories;
......
......@@ -25,6 +25,8 @@
#include "vtkPeriodicTable.h"
#include "vtkXMLParser.h"
#include "vtksys/SystemTools.hxx"
#include <string>
#include <vector>
......@@ -254,11 +256,12 @@ void vtkCMLParser::NewBond(const char **attr)
// Get names of bonded atoms
if (strcmp(cur, "atomRefs2") == 0)
{
char atomRefs[128];
strncpy(atomRefs, attr[++attrInd], 128);
std::string atomRefs = attr[++attrInd];
std::vector<std::string> words;
// Parse out atom names:
const char *nameChar = strtok(atomRefs, " ");
while (nameChar != nullptr)
vtksys::SystemTools::Split(atomRefs, words, ' ');
std::vector<std::string>::const_iterator words_iter = words.begin();
while (words_iter != words.end())
{
vtkIdType currentAtomId;
bool found = false;
......@@ -266,7 +269,7 @@ void vtkCMLParser::NewBond(const char **attr)
currentAtomId < static_cast<vtkIdType>(this->AtomNames.size());
++currentAtomId)
{
if (this->AtomNames[currentAtomId].compare(nameChar) == 0)
if (this->AtomNames[currentAtomId].compare(*words_iter) == 0)
{
found = true;
break;
......@@ -282,10 +285,10 @@ void vtkCMLParser::NewBond(const char **attr)
allAtomNames.push_back(' ');
}
vtkWarningMacro(<< "NewBond(): unknown atom name '"
<< nameChar << "'. Known atoms:\n"
<< allAtomNames.c_str());
<< *words_iter << "'. Known atoms:\n"
<< allAtomNames);
nameChar = strtok(nullptr, " ");
++words_iter;
continue;
}
else if (atomId1 == -1)
......@@ -302,7 +305,7 @@ void vtkCMLParser::NewBond(const char **attr)
<< atomRefs);
}
nameChar = strtok(nullptr, " ");
++words_iter;
}
}
......
......@@ -68,8 +68,7 @@ vtkNIFTIImageWriter::vtkNIFTIImageWriter()
size_t l = strlen(version);
this->Description = new char[l + 4];
memcpy(this->Description, "VTK", 3);
strncpy(&this->Description[3], version, l);
this->Description[l + 3] = '\0';
strcpy(&this->Description[3], version);
// Planar RGB (NIFTI doesn't allow this, it's here for Analyze)
this->PlanarRGB = false;
}
......
......@@ -895,8 +895,7 @@ void vtkMINCImageAttributes::SetAttributeValueAsString(
// Allocate an extra byte to store a null terminator.
array->Resize(static_cast<vtkIdType>(length + 1));
char *dest = array->WritePointer(0, static_cast<vtkIdType>(length));
strncpy(dest, value, length);
dest[length] = '\0';
strcpy(dest, value);
this->SetAttributeValueAsArray(variable, attribute, array);
array->Delete();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment