[Mesa-dev] Mesa (master): glapi: s/strcpy/strncpy/

Brian Paul brianp at vmware.com
Mon May 3 15:09:07 PDT 2010


Ian Romanick wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Ian Romanick wrote:
>> Vinson Lee wrote:
>>> Module: Mesa
>>> Branch: master
>>> Commit: 9446fd8f69564e09ffd0f28735a99c510f84bb62
>>> URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=9446fd8f69564e09ffd0f28735a99c510f84bb62
>>> Author: Vinson Lee <vlee at vmware.com>
>>> Date:   Sat May  1 15:34:47 2010 -0700
>>> glapi: s/strcpy/strncpy/
>>> ---
>>>  src/mesa/glapi/glapi_getproc.c |    3 ++-
>>>  1 files changed, 2 insertions(+), 1 deletions(-)
>>> diff --git a/src/mesa/glapi/glapi_getproc.c b/src/mesa/glapi/glapi_getproc.c
>>> index c73e8dd..ec96ab3 100644
>>> --- a/src/mesa/glapi/glapi_getproc.c
>>> +++ b/src/mesa/glapi/glapi_getproc.c
>>> @@ -265,7 +265,8 @@ str_dup(const char *str)
>>>     copy = (char*) malloc(strlen(str) + 1);
>>>     if (!copy)
>>>        return NULL;
>>> -   strcpy(copy, str);
>>> +   strncpy(copy, str, strlen(str));
>>> +   copy[strlen(str)] = '\0';
>>>     return copy;
>>>  }
> 
> It occurs to me that if the goal is to get rid of strcpy, presumably to
> silence a static analysis tool, the code could be changed to use memcpy:
> 
> 	const size_t len = strlen(str) + 1;
> 	char *const copy = (char *) malloc(len);
> 	if (!copy)
> 		return NULL;
> 	memcpy(copy, str, len);
> 	return copy;

That looks good.


> My guess is that the code GCC actually generates for both versions (the
> original and the memcpy version) is pretty close to identical.  At -O0,
> the memcpy version should be smaller / faster / have better stickers.
> 
> In this particular case, why aren't we just using the system strdup?
> Are there really still platforms that don't include it?

IIRC, Windows.

-Brian


More information about the mesa-dev mailing list