[PATCH:libFS 1/2] Change FSOpenServer and FSServerName to take const char * args.

Alan Coopersmith alan.coopersmith at oracle.com
Sun Nov 13 09:39:04 PST 2011


On 11/12/11 02:08, Mark Kettenis wrote:
>> From: Alan Coopersmith<alan.coopersmith at oracle.com>
>> Date: Fri, 11 Nov 2011 21:43:04 -0800
>
>> diff --git a/src/FSServName.c b/src/FSServName.c
>> index 022be82..a535b38 100644
>> --- a/src/FSServName.c
>> +++ b/src/FSServName.c
>> @@ -59,12 +59,12 @@ in this Software without prior written authorization from The Open Group.
>>   #include	"FSlib.h"
>>
>>   char       *
>> -FSServerName(char *server)
>> +FSServerName(const char *server)
>
> I think it is wrong to make it accept a const string...
>
>>   {
>>       char       *s;
>>
>>       if (server != NULL&&  *server != '\0')
>> -	return server;
>> +	return (char *) server;
>
> ...you're then going to return a non-const version of it.

I had merely copied what was done for years in XDisplayName(), the Xlib
function this was copied from.   Presumably when Xlib had const added
(since these API's all predate when const support could be assumed to
  be present in all compilers and were later retrofitted), they decided
it was too risky to change the return type to const and cause problems
for callers who were storing it in a non-const variable.

The C Standard Library is full of similar const holes, where you pass
a const string to something like strchr and get a non-const one back,
since C doesn't have the ability to choose different function variants
for const vs. non-const arguments that C++ does.

However, in this case, we probably could change the return type, as
this is the library for talking the font server, and as far as I know,
all 5 callers of libFS are in our control and easy to fix simultaneously.
At a quick glance, only one (fstobdf) even stores the result in a variable,
so I'll go ahead and do that.

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



More information about the xorg-devel mailing list