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 3 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;
size_t nStringLength = strlen(pszString) + 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.

the declaration and its affectation of both variables should be separated. Namely after the if (pszString == NULL) pszString = ""; check
You're close ;-)

char *pszReturn = malloc(nStringLength);

if( pszString == NULL )
pszString = "";
if (pszString == NULL)
pszString = "";

pszReturn = strdup( pszString );
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);
}

if( pszReturn == NULL ) {
fprintf(stderr, "msSmallMsStrdup(): Out of memory allocating %ld bytes.\n",
(long) strlen(pszString) );
exit(1);
}
memcpy(pszReturn, pszString, nStringLength);

return( pszReturn );
return pszReturn;
}


Expand Down