[PATCH 09/11] Convert malloc+sprintf pairs into X*asprintf() calls

Alan Coopersmith alan.coopersmith at oracle.com
Tue Nov 30 15:15:22 PST 2010


Julien Cristau wrote:
> On Mon, Nov 29, 2010 at 20:57:46 -0800, Alan Coopersmith wrote:
> 
>> diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c
>> index 06d082b..751fec6 100644
>> --- a/hw/xfree86/loader/loadmod.c
>> +++ b/hw/xfree86/loader/loadmod.c
>> @@ -406,22 +406,22 @@ FindModuleInSubdir(const char *dirpath, const char *module)
>>   
>>          snprintf(tmpBuf, PATH_MAX, "lib%s.so", module);
>>          if (strcmp(direntry->d_name, tmpBuf) == 0) {
>> -            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1);
>> -            sprintf(ret, "%s%s", dirpath, tmpBuf);
>> +            if (Xasprintf(&ret, "%s%s", dirpath, tmpBuf) == -1)
>> +		ret = NULL;
>>              break;
>>          }
>>  
>>          snprintf(tmpBuf, PATH_MAX, "%s_drv.so", module);
>>          if (strcmp(direntry->d_name, tmpBuf) == 0) {
>> -            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1);
>> -            sprintf(ret, "%s%s", dirpath, tmpBuf);
>> +            if (Xasprintf(&ret, "%s%s", dirpath, tmpBuf) == -1)
>> +		ret = NULL;
>>              break;
>>          }
>>  
>>          snprintf(tmpBuf, PATH_MAX, "%s.so", module);
>>          if (strcmp(direntry->d_name, tmpBuf) == 0) {
>> -            ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1);
>> -            sprintf(ret, "%s%s", dirpath, tmpBuf);
>> +            if (Xasprintf(&ret, "%s%s", dirpath, tmpBuf) == -1)
>> +		ret = NULL;
>>              break;
>>          }
>>      }
> 
> Should these use XNFasprintf?

Certainly the previous code effectively did that (though by just SEGV on
writing to the NULL value returned by malloc, instead of an orderly abort),
but since the callers handle NULL return values already, it seemed like
returning failure was better than killing the server.   Not that you can
get much farther into initialization if you run out of memory for module
path strings while just loading the modules.

-- 
	-Alan Coopersmith-        alan.coopersmith at oracle.com
	 Oracle Solaris Platform Engineering: X Window System



More information about the xorg-devel mailing list