[PATCH:libXt 3/3] Convert ALLOCATE_LOCAL + sprintf to XtAsprintf

Alan Coopersmith alan.coopersmith at oracle.com
Mon Mar 7 15:39:52 PST 2011


On 03/ 7/11 10:57 AM, Cyril Brulebois wrote:
> Hi Alan,
> 
> Alan Coopersmith <alan.coopersmith at oracle.com> (12/01/2011):
>> Now that we don't need to separately strlen the format string, put it
>> directly into the function call to clear gcc warnings of:
>> Initialize.c:397: warning: format not a string literal, argument types not checked
>>
>> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
>> ---
>>  src/Initialize.c |   25 ++++++++++---------------
>>  1 files changed, 10 insertions(+), 15 deletions(-)
>>
>> diff --git a/src/Initialize.c b/src/Initialize.c
>> index 2e70e39..e9543da 100644
>> --- a/src/Initialize.c
>> +++ b/src/Initialize.c
>> @@ -380,7 +380,7 @@ static void CombineAppUserDefaults(
>>      XrmDatabase *pdb)
>>  {
>>      char* filename;
>> -    char* path;
>> +    char* path = NULL;
>>      Boolean deallocate = False;
>>  
>>      if (!(path = getenv("XUSERFILESEARCHPATH"))) {
>> @@ -389,20 +389,14 @@ static void CombineAppUserDefaults(
>>  	char homedir[PATH_MAX];
>>  	GetRootDirName(homedir, PATH_MAX);
>>  	if (!(old_path = getenv("XAPPLRESDIR"))) {
>> -	    char *path_default = "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
>> -	    if (!(path =
>> -		  ALLOCATE_LOCAL(6*strlen(homedir) + strlen(path_default))))
>> -		_XtAllocError(NULL);
>> -	    sprintf( path, path_default,
>> -		    homedir, homedir, homedir, homedir, homedir, homedir );
>> +	    XtAsprintf(&path,
>> +		       "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N",
>> +		       homedir, homedir, homedir, homedir, homedir, homedir);
>>  	} else {
>> -	    char *path_default = "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
>> -	    if (!(path =
>> -		  ALLOCATE_LOCAL( 6*strlen(old_path) + 2*strlen(homedir)
>> -				 + strlen(path_default))))
>> -		_XtAllocError(NULL);
>> -	    sprintf(path, path_default, old_path, old_path, old_path, homedir,
>> -		    old_path, old_path, old_path, homedir );
>> +	    XtAsprintf(&path,
>> +		       "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N",
>> +		       old_path, old_path, old_path, homedir,
>> +		       old_path, old_path, old_path, homedir);
>>  	}
>>  	deallocate = True;
>>  #endif
>> @@ -414,7 +408,8 @@ static void CombineAppUserDefaults(
>>  	XtFree(filename);
>>      }
>>  
>> -    if (deallocate) DEALLOCATE_LOCAL(path);
>> +    if (deallocate)
>> +	XtFree(path);
> 
> 
> I have yet to figure out why, but this breaks xdm when starting it
> from init (but not through a /etc/init.d/xdm restart).

Perhaps a difference in the setting of $HOME or one of the other environment
variables that GetRootDirName checks for the homedir?

I can't see why that would make XtAsprintf operate any differently though.

> Reverting that commit makes xdm start from init, as well as replacing
> XtAsprintf with asprintf (with a #define _GNU_SOURCE before including
> <stdio.h>).
> 
> I also tried reinstating error checking (if XtAsprintf returns -1,
> call _XtAllocError(NULL)), but that didn't change anything (I assumed
> that “like asprintf” in the git log meant that return value would
> match asprintf's).

Except that like XtMalloc etal, XtAsprintf calls the Xt error handling
code and exits on failure.

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



More information about the xorg-devel mailing list