Skip to content

Commit

Permalink
Replaced strcpy by OFStandard::strlcpy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Eichelberg committed May 5, 2018
1 parent aa66992 commit 4730893
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 45 deletions.
4 changes: 2 additions & 2 deletions dcmdata/include/dcmtk/dcmdata/dcdirrec.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1994-2016, OFFIS e.V.
* Copyright (C) 1994-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand Down Expand Up @@ -461,7 +461,7 @@ class DCMTK_DCMDATA_EXPORT DcmDirectoryRecord : public DcmItem

// side-effect-free conversion routines:
E_DirRecType recordNameToType(const char *recordTypeName);
char* buildFileName(const char *origName, char *destName);
char* buildFileName(const char *origName, char *destName, size_t len) const;
OFCondition checkHierarchy(const E_DirRecType upperRecord,
const E_DirRecType lowerRecord);

Expand Down
13 changes: 8 additions & 5 deletions dcmdata/libsrc/cmdlnarg.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1996-2017, OFFIS e.V.
* Copyright (C) 1996-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand Down Expand Up @@ -41,6 +41,7 @@
#include "dcmtk/ofstd/ofstdinc.h"

#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/ofstd/ofstd.h"

void prepareCmdLineArgs(int& argc, char* argv[],
const char* progname)
Expand All @@ -49,8 +50,9 @@ void prepareCmdLineArgs(int& argc, char* argv[],
char buf[bufsize];
char arg[1024];

argv[0] = new char[strlen(progname)+1];
strcpy(argv[0], progname);
size_t len = strlen(progname)+1;
argv[0] = new char[len];
OFStandard::strlcpy(argv[0], progname, len);
argc = 1;

ofConsole.lockCout() << "CmdLineArgs-> ";
Expand All @@ -63,8 +65,9 @@ void prepareCmdLineArgs(int& argc, char* argv[],
while (is.good()) {
is >> arg;
if (strlen(arg) > 0) {
argv[argc] = new char[strlen(arg)+1];
strcpy(argv[argc], arg);
size_t len = strlen(arg)+1;
argv[argc] = new char[len];
OFStandard::strlcpy(argv[argc], arg, len);
argc++;
}
arg[0] = '\0';
Expand Down
8 changes: 5 additions & 3 deletions dcmdata/libsrc/dcdicent.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1994-2010, OFFIS e.V.
* Copyright (C) 1994-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand All @@ -26,6 +26,7 @@
#include "dcmtk/ofstd/ofstdinc.h"

#include "dcmtk/dcmdata/dcdicent.h"
#include "dcmtk/ofstd/ofstd.h"

/*
** DcmDictEntry member functions
Expand All @@ -36,8 +37,9 @@ char* strdup_new(const char* str)
{
char* s = NULL;
if (str != NULL) {
s = new char[strlen(str)+1];
strcpy(s, str);
size_t len = strlen(str)+1;
s = new char[len];
OFStandard::strlcpy(s, str, len);
}
return s;
}
Expand Down
8 changes: 5 additions & 3 deletions dcmdata/libsrc/dcdict.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1994-2017, OFFIS e.V.
* Copyright (C) 1994-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand All @@ -27,6 +27,7 @@
#include "dcmtk/ofstd/ofdefine.h"
#include "dcmtk/dcmdata/dcdicent.h"
#include "dcmtk/dcmdata/dctypes.h"
#include "dcmtk/ofstd/ofstd.h"

#define INCLUDE_CSTDLIB
#define INCLUDE_CSTDIO
Expand Down Expand Up @@ -360,8 +361,9 @@ parseWholeTagField(char* s, DcmTagKey& key,
if (pi > 0)
{
// copy private creator name
privCreator = new char[strlen(pc) + 1]; // deleted by caller
if (privCreator) strcpy(privCreator,pc);
size_t buflen = strlen(pc) + 1;
privCreator = new char[buflen]; // deleted by caller
if (privCreator) OFStandard::strlcpy(privCreator, pc, buflen);
}

key.set(OFstatic_cast(unsigned short, gl), OFstatic_cast(unsigned short, el));
Expand Down
30 changes: 18 additions & 12 deletions dcmdata/libsrc/dcdirrec.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1994-2017, OFFIS e.V.
* Copyright (C) 1994-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand Down Expand Up @@ -32,6 +32,7 @@

#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/ofstd/ofcast.h"
#include "dcmtk/ofstd/ofstd.h"

#include "dcmtk/dcmdata/dcdirrec.h"
#include "dcmtk/dcmdata/dctk.h"
Expand Down Expand Up @@ -270,7 +271,8 @@ E_DirRecType DcmDirectoryRecord::recordNameToType(const char *recordTypeName)


char *DcmDirectoryRecord::buildFileName(const char *origName,
char *destName)
char *destName,
size_t len) const
{
const char *from = origName;
char *to = destName;
Expand Down Expand Up @@ -298,13 +300,14 @@ char *DcmDirectoryRecord::buildFileName(const char *origName,
{
fclose(f);
} else {
char* newname = new char[strlen(destName) + 2];
strcpy(newname, destName);
strcat(newname, ".");
size_t buflen = strlen(destName) + 2;
char* newname = new char[buflen];
OFStandard::strlcpy(newname, destName, buflen);
OFStandard::strlcat(newname, ".", buflen);
if ((f = fopen(newname, "rb")) != NULL)
{
fclose(f);
strcpy(destName, newname);
OFStandard::strlcpy(destName, newname, len);
} else {
/* we cannot find the file. let the caller deal with this */
}
Expand Down Expand Up @@ -625,8 +628,9 @@ OFCondition DcmDirectoryRecord::setReferencedFileID(const char *referencedFileID
{
OFCondition l_error = EC_Normal;

char* newFname = new char[strlen(referencedFileID) + 1];
strcpy(newFname, referencedFileID);
size_t bufsize = strlen(referencedFileID) + 1;
char* newFname = new char[bufsize];
OFStandard::strlcpy(newFname, referencedFileID, bufsize);
hostToDicomFilename(newFname);

DcmTag refFileTag(DCM_ReferencedFileID);
Expand Down Expand Up @@ -908,8 +912,9 @@ OFCondition DcmDirectoryRecord::fillElementsAndReadSOP(const char *referencedFil
if (sourceFileName.isEmpty())
{
/* create a new source filename */
char *newname = new char[strlen(referencedFileID) + 2];
buildFileName(referencedFileID, newname);
size_t bufsize = strlen(referencedFileID) + 2;
char *newname = new char[bufsize];
buildFileName(referencedFileID, newname, bufsize);
fileName.set(newname);
delete[] newname;
} else {
Expand Down Expand Up @@ -1079,8 +1084,9 @@ OFCondition DcmDirectoryRecord::purgeReferencedFile()
const char *fileName = lookForReferencedFileID();
if (fileName != NULL)
{
localFileName = new char[strlen(fileName) + 2];
buildFileName(fileName, localFileName);
size_t buflen = strlen(fileName) + 2;
localFileName = new char[buflen];
buildFileName(fileName, localFileName, buflen);
setReferencedFileID(NULL);
}

Expand Down
13 changes: 8 additions & 5 deletions dcmdata/libsrc/dctag.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1994-2011, OFFIS e.V.
* Copyright (C) 1994-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand All @@ -24,6 +24,7 @@
#include "dcmtk/dcmdata/dcerror.h" /* for dcmdata error constants */
#include "dcmtk/dcmdata/dcdict.h"
#include "dcmtk/dcmdata/dcdicent.h"
#include "dcmtk/ofstd/ofstd.h"

#define INCLUDE_CSTDIO
#define INCLUDE_CSTRING
Expand Down Expand Up @@ -248,9 +249,10 @@ void DcmTag::updateTagName(const char *c)
delete[] tagName;
if (c)
{
tagName = new char[strlen(c) + 1];
size_t buflen = strlen(c) + 1;
tagName = new char[buflen];
if (tagName)
strcpy(tagName, c);
OFStandard::strlcpy(tagName, c, buflen);
} else
tagName = NULL;
}
Expand All @@ -260,9 +262,10 @@ void DcmTag::updatePrivateCreator(const char *c)
delete[] privateCreator;
if (c)
{
privateCreator = new char[strlen(c) + 1];
size_t buflen = strlen(c) + 1;
privateCreator = new char[buflen];
if (privateCreator)
strcpy(privateCreator, c);
OFStandard::strlcpy(privateCreator, c, buflen);
} else
privateCreator = NULL;
}
3 changes: 2 additions & 1 deletion dcmdata/libsrc/dctagkey.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmdata/dctagkey.h"
#include "dcmtk/ofstd/ofstd.h"

#define INCLUDE_CSTDIO
#define INCLUDE_CSTRING
Expand Down Expand Up @@ -49,7 +50,7 @@ OFString DcmTagKey::toString() const

if (group == 0xffff && element == 0xffff)
{
strcpy(tagBuf, "(\?\?\?\?,\?\?\?\?)"); // prevent trigraph expansion in string constant
OFStandard::strlcpy(tagBuf, "(\?\?\?\?,\?\?\?\?)", 16); // prevent trigraph expansion in string constant
} else {
sprintf(tagBuf, "(%04x,%04x)", OFstatic_cast(unsigned, group), OFstatic_cast(unsigned, element));
}
Expand Down
2 changes: 1 addition & 1 deletion dcmdata/libsrc/mkdeftag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ printDefined(FILE* fout, const DcmDictEntry* e)
if (e == NULL || e->getTagName() == NULL)
return;

strcpy(buf, e->getTagName());
OFStandard::strlcpy(buf, e->getTagName(), DCM_MAXDICTLINESIZE+1);

convertToIdentifier(buf);

Expand Down
4 changes: 2 additions & 2 deletions ofstd/libsrc/offname.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1997-2017, OFFIS e.V.
* Copyright (C) 1997-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand Down Expand Up @@ -112,7 +112,7 @@ void OFFilenameCreator::addLongToString(unsigned long l, OFString &s)
unsigned long m;
int idx=7;
char chr_array[9];
strcpy(chr_array, "00000000");
OFStandard::strlcpy(chr_array, "00000000", 9);
while (l)
{
m = l & 0x0FL;
Expand Down
19 changes: 11 additions & 8 deletions ofstd/libsrc/ofstd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -804,16 +804,17 @@ OFFilename &OFStandard::combineDirAndFilename(OFFilename &result,
else {
const char *resValue = result.getCharPointer();
const size_t resLength = strlen(resValue); /* should never be 0 */
char *tmpString = new char[strLength + resLength + 1 + 1];
strcpy(tmpString, resValue);
const size_t buflen = strLength + resLength + 1 + 1;
char *tmpString = new char[buflen];
OFStandard::strlcpy(tmpString, resValue, buflen);
/* add path separator (if required) ... */
if (resValue[resLength - 1] != PATH_SEPARATOR)
{
tmpString[resLength] = PATH_SEPARATOR;
tmpString[resLength + 1] = '\0';
}
/* ...and file name */
strcat(tmpString, strValue);
OFStandard::strlcat(tmpString, strValue, buflen);
result.set(tmpString);
delete[] tmpString;
}
Expand Down Expand Up @@ -895,9 +896,10 @@ OFCondition OFStandard::removeRootDirFromPathname(OFFilename &result,
if (strncmp(rootValue, pathValue, rootLength) == 0)
{
/* create temporary buffer for destination string */
char *tmpString = new char[pathLength - rootLength + 1];
size_t buflen = pathLength - rootLength + 1;
char *tmpString = new char[buflen];
/* remove root dir prefix from path name */
strcpy(tmpString, pathValue + rootLength);
OFStandard::strlcpy(tmpString, pathValue + rootLength, buflen);
/* remove leading path separator (if present) */
if (!allowLeadingPathSeparator && (tmpString[0] == PATH_SEPARATOR))
result.set(tmpString + 1);
Expand Down Expand Up @@ -947,10 +949,11 @@ OFFilename &OFStandard::appendFilenameExtension(OFFilename &result,
size_t namLength = (namValue == NULL) ? 0 : strlen(namValue);
size_t extLength = (extValue == NULL) ? 0 : strlen(extValue);
/* create temporary buffer for destination string */
char *tmpString = new char[namLength + extLength + 1];
strcpy(tmpString, (namValue == NULL) ? "" : namValue);
size_t buflen = namLength + extLength + 1;
char *tmpString = new char[buflen];
OFStandard::strlcpy(tmpString, (namValue == NULL) ? "" : namValue, buflen);
if (extValue != NULL)
strcat(tmpString, extValue);
OFStandard::strlcat(tmpString, extValue, buflen);
result.set(tmpString);
delete[] tmpString;
}
Expand Down
9 changes: 6 additions & 3 deletions ofstd/libsrc/ofstring.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 1997-2017, OFFIS e.V.
* Copyright (C) 1997-2018, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
Expand Down Expand Up @@ -36,6 +36,7 @@
#include "dcmtk/ofstd/ofcast.h"
#include "dcmtk/ofstd/ofbmanip.h"
#include "dcmtk/ofstd/oftypes.h"
#include "dcmtk/ofstd/ofstd.h"

#define INCLUDE_CCTYPE
#include "dcmtk/ofstd/ofstdinc.h"
Expand Down Expand Up @@ -89,9 +90,11 @@ OFString::OFString (const char* s)
s = verify_string(s);
const size_t n = strlen(s);
reserve(n);
// Because we used strlen() to figure out the length we can use strcpy()
// Because we used strlen() to figure out the length we can use strlcpy()
// since there won't be any '\0' bytes in the string.
strcpy(this->theCString, s);
// The amount of memory allocated is always theCapacity+1
// because one extra byte is always allocated for the eos zero byte.
OFStandard::strlcpy(this->theCString, s, this->theCapacity+1);
this->theSize = n;
}

Expand Down

0 comments on commit 4730893

Please sign in to comment.