Skip to content
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

Merged
merged 4 commits into from Jun 15, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions mapstring.c
Expand Up @@ -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);
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

size_t nStringLength;
char* pszReturn;

char *pszReturn = malloc(nStringLength + 1);
memcpy(pszReturn, pszString, nStringLength + 1); /* null terminated byte*/
Copy link
Contributor

Choose a reason for hiding this comment

The 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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}


Expand Down