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

Ian Romanick idr at freedesktop.org
Mon May 3 15:01:23 PDT 2010


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

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?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkvfR7EACgkQX1gOwKyEAw+UuACfVHv0dKlfLFZ/XVWCvgBOizMJ
/mEAnR/dh4jNbRfkkBu+7acuw4fojj82
=pPIJ
-----END PGP SIGNATURE-----


More information about the mesa-dev mailing list