New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use strcpy rather than strdup #5610
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2103,22 +2103,23 @@ int msStringIsInteger(const char *string) | |
|
||
/* Safe version of msStrdup(). This function is taken from gdal/cpl. */ | ||
|
||
char *msStrdup( const char * pszString ) | ||
char *msStrdup(const char * pszString) | ||
{ | ||
char *pszReturn; | ||
|
||
if( pszString == NULL ) | ||
pszString = ""; | ||
if (pszString == NULL) | ||
pszString = ""; | ||
|
||
pszReturn = strdup( pszString ); | ||
unsigned int nStringLength = strlen(pszString); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Being in a .c file, the variable declarations should be done at the beginning of the function to please MSVC
|
||
char *pszReturn = malloc(nStringLength + 1); | ||
memcpy(pszReturn, pszString, nStringLength + 1); /* null terminated byte*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memcpy() should be done after the below NULL check |
||
|
||
if( pszReturn == NULL ) { | ||
fprintf(stderr, "msSmallMsStrdup(): Out of memory allocating %ld bytes.\n", | ||
(long) strlen(pszString) ); | ||
exit(1); | ||
} | ||
if (pszReturn == NULL) { | ||
fprintf(stderr, "msSmallMalloc(): Out of memory allocating %ld bytes.\n", | ||
(long)strlen(pszString)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strlen(pszString) could be replaced by nStringLength |
||
exit(1); | ||
} | ||
|
||
return( pszReturn ); | ||
return pszReturn; | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use size_t for type safety instead of unsigned int