Commit 9c6f5402 authored by Ben Boeckel's avatar Ben Boeckel Committed by Kitware Robot
Browse files

Merge topic 'fix-warnings-20180905'

e1aa3933

 warnings: fix strncpy warnings
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !4647
parents 7621d322 e1aa3933
...@@ -108,15 +108,18 @@ void vtkObjectFactory::LoadDynamicFactories() ...@@ -108,15 +108,18 @@ void vtkObjectFactory::LoadDynamicFactories()
#ifndef _WIN32_WCE #ifndef _WIN32_WCE
LoadPath = getenv("VTK_AUTOLOAD_PATH"); LoadPath = getenv("VTK_AUTOLOAD_PATH");
#endif #endif
if(LoadPath == nullptr || strlen(LoadPath) == 0) if(LoadPath == nullptr || LoadPath[0] == 0)
{ {
return; return;
} }
char* CurrentPath = new char[strlen(LoadPath)+1]; std::string CurrentPath;
CurrentPath.reserve(strlen(LoadPath) + 1);
char* SeparatorPosition = LoadPath; // initialize to env variable char* SeparatorPosition = LoadPath; // initialize to env variable
while(SeparatorPosition) while(SeparatorPosition)
{ {
size_t PathLength =0; CurrentPath.clear();
size_t PathLength = 0;
// find PathSeparator in LoadPath // find PathSeparator in LoadPath
SeparatorPosition = strchr(LoadPath, PathSeparator); SeparatorPosition = strchr(LoadPath, PathSeparator);
// if not found then use the whole string // if not found then use the whole string
...@@ -129,23 +132,19 @@ void vtkObjectFactory::LoadDynamicFactories() ...@@ -129,23 +132,19 @@ void vtkObjectFactory::LoadDynamicFactories()
PathLength = static_cast<size_t>(SeparatorPosition - LoadPath); PathLength = static_cast<size_t>(SeparatorPosition - LoadPath);
} }
// copy the path out of LoadPath into CurrentPath // copy the path out of LoadPath into CurrentPath
strncpy(CurrentPath, LoadPath, PathLength); CurrentPath.append(LoadPath, PathLength);
// add a null terminator
CurrentPath[PathLength] = 0;
// Get ready for the next path // Get ready for the next path
LoadPath = SeparatorPosition+1; LoadPath = SeparatorPosition + 1;
// Load the libraries in the current path // Load the libraries in the current path
vtkObjectFactory::LoadLibrariesInPath(CurrentPath); vtkObjectFactory::LoadLibrariesInPath(CurrentPath);
} }
// clean up memory
delete [] CurrentPath;
} }
// A file scope helper function to concat path and file into // A file scope helper function to concat path and file into
// a full path // 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]; char* ret = new char[lenpath + strlen(file)+2];
#ifdef _WIN32 #ifdef _WIN32
const char sep = '\\'; const char sep = '\\';
...@@ -153,7 +152,7 @@ static char* CreateFullPath(const char* path, const char* file) ...@@ -153,7 +152,7 @@ static char* CreateFullPath(const char* path, const char* file)
const char sep = '/'; const char sep = '/';
#endif #endif
// make sure the end of path is a separator // make sure the end of path is a separator
strcpy(ret, path); strcpy(ret, path.c_str());
if(ret[lenpath-1] != sep) if(ret[lenpath-1] != sep)
{ {
ret[lenpath] = sep; ret[lenpath] = sep;
...@@ -190,7 +189,7 @@ inline int vtkNameIsSharedLibrary(const char* name) ...@@ -190,7 +189,7 @@ inline int vtkNameIsSharedLibrary(const char* name)
return (ret != nullptr); return (ret != nullptr);
} }
void vtkObjectFactory::LoadLibrariesInPath(const char* path) void vtkObjectFactory::LoadLibrariesInPath(const std::string& path)
{ {
vtksys::Directory dir; vtksys::Directory dir;
if(!dir.Load(path)) if(!dir.Load(path))
......
...@@ -263,7 +263,7 @@ private: ...@@ -263,7 +263,7 @@ private:
/** /**
* Load all dynamic libraries in the given path * Load all dynamic libraries in the given path
*/ */
static void LoadLibrariesInPath( const char*); static void LoadLibrariesInPath(const std::string&);
// list of registered factories // list of registered factories
static vtkObjectFactoryCollection* RegisteredFactories; static vtkObjectFactoryCollection* RegisteredFactories;
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include "vtkPeriodicTable.h" #include "vtkPeriodicTable.h"
#include "vtkXMLParser.h" #include "vtkXMLParser.h"
#include "vtksys/SystemTools.hxx"
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -254,11 +256,12 @@ void vtkCMLParser::NewBond(const char **attr) ...@@ -254,11 +256,12 @@ void vtkCMLParser::NewBond(const char **attr)
// Get names of bonded atoms // Get names of bonded atoms
if (strcmp(cur, "atomRefs2") == 0) if (strcmp(cur, "atomRefs2") == 0)
{ {
char atomRefs[128]; std::string atomRefs = attr[++attrInd];
strncpy(atomRefs, attr[++attrInd], 128); std::vector<std::string> words;
// Parse out atom names: // Parse out atom names:
const char *nameChar = strtok(atomRefs, " "); vtksys::SystemTools::Split(atomRefs, words, ' ');
while (nameChar != nullptr) std::vector<std::string>::const_iterator words_iter = words.begin();
while (words_iter != words.end())
{ {
vtkIdType currentAtomId; vtkIdType currentAtomId;
bool found = false; bool found = false;
...@@ -266,7 +269,7 @@ void vtkCMLParser::NewBond(const char **attr) ...@@ -266,7 +269,7 @@ void vtkCMLParser::NewBond(const char **attr)
currentAtomId < static_cast<vtkIdType>(this->AtomNames.size()); currentAtomId < static_cast<vtkIdType>(this->AtomNames.size());
++currentAtomId) ++currentAtomId)
{ {
if (this->AtomNames[currentAtomId].compare(nameChar) == 0) if (this->AtomNames[currentAtomId].compare(*words_iter) == 0)
{ {
found = true; found = true;
break; break;
...@@ -282,10 +285,10 @@ void vtkCMLParser::NewBond(const char **attr) ...@@ -282,10 +285,10 @@ void vtkCMLParser::NewBond(const char **attr)
allAtomNames.push_back(' '); allAtomNames.push_back(' ');
} }
vtkWarningMacro(<< "NewBond(): unknown atom name '" vtkWarningMacro(<< "NewBond(): unknown atom name '"
<< nameChar << "'. Known atoms:\n" << *words_iter << "'. Known atoms:\n"
<< allAtomNames.c_str()); << allAtomNames);
nameChar = strtok(nullptr, " "); ++words_iter;
continue; continue;
} }
else if (atomId1 == -1) else if (atomId1 == -1)
...@@ -302,7 +305,7 @@ void vtkCMLParser::NewBond(const char **attr) ...@@ -302,7 +305,7 @@ void vtkCMLParser::NewBond(const char **attr)
<< atomRefs); << atomRefs);
} }
nameChar = strtok(nullptr, " "); ++words_iter;
} }
} }
......
...@@ -68,8 +68,7 @@ vtkNIFTIImageWriter::vtkNIFTIImageWriter() ...@@ -68,8 +68,7 @@ vtkNIFTIImageWriter::vtkNIFTIImageWriter()
size_t l = strlen(version); size_t l = strlen(version);
this->Description = new char[l + 4]; this->Description = new char[l + 4];
memcpy(this->Description, "VTK", 3); memcpy(this->Description, "VTK", 3);
strncpy(&this->Description[3], version, l); strcpy(&this->Description[3], version);
this->Description[l + 3] = '\0';
// Planar RGB (NIFTI doesn't allow this, it's here for Analyze) // Planar RGB (NIFTI doesn't allow this, it's here for Analyze)
this->PlanarRGB = false; this->PlanarRGB = false;
} }
......
...@@ -895,8 +895,7 @@ void vtkMINCImageAttributes::SetAttributeValueAsString( ...@@ -895,8 +895,7 @@ void vtkMINCImageAttributes::SetAttributeValueAsString(
// Allocate an extra byte to store a null terminator. // Allocate an extra byte to store a null terminator.
array->Resize(static_cast<vtkIdType>(length + 1)); array->Resize(static_cast<vtkIdType>(length + 1));
char *dest = array->WritePointer(0, static_cast<vtkIdType>(length)); char *dest = array->WritePointer(0, static_cast<vtkIdType>(length));
strncpy(dest, value, length); strcpy(dest, value);
dest[length] = '\0';
this->SetAttributeValueAsArray(variable, attribute, array); this->SetAttributeValueAsArray(variable, attribute, array);
array->Delete(); 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