bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sharutils-4.11.1] Compilation warnings


From: Eric Blake
Subject: Re: [sharutils-4.11.1] Compilation warnings
Date: Thu, 06 Dec 2012 17:29:46 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0

On 12/06/2012 03:02 PM, Bruce Korb wrote:

>> genshell.c:391:13: warning: assignment discards 'const' qualifier from 
>> pointer target type [enabled by default]
> 
> Having AO_gettext return "char const *" is too difficult.
> "_" is a macro, so I'm coerce casting its result to "char *":

You can also shut up the compiler by doing tricks like
 strstr((const char *)expr, "")
although I'm not sure they are worth it.

> 
>>     res = (char *)(void *)_(pz);
> 
>> In file included from libopts.c:33:0:
>> pgusage.c: In function 'optionPagedUsage':
>> pgusage.c:67:9: warning: format '%lu' expects argument of type 'long 
>> unsigned int', but argument 4 has type 'long long unsigned int' [-Wformat]
> 
> Yummy.  The type of the value was cast to uint64_t, but I guess I don't know 
> what
> that should map to.  So I'll cast the value to "unsigned long".  Since the 
> value
> is actually a PID number, I doubt it gets past 4 billion on 32 bit platforms.

While you are correct that it is 32-bit on most platforms (all 32-bit
platforms, and most 64-bit platforms), your idea of casting to 'unsigned
long' is not portable to mingw64, where 'unsigned long' is 32 bits but
'pid_t' is 64 bits.  The only portable solution here is to cast to
'[u]intmax_t'.

> 
>> pgusage.c:112:9: warning: ignoring return value of 'system', declared with 
>> attribute warn_unused_result [-Wunused-result]
> 
>>     112         (void)system(fil_name);
>>     113     }
> 
> I explicitly cast the return value to void.  What does the blinking compiler 
> want?

To warn you that you are blatantly ignoring the chance of failure.

> If the command succeeds, cool.  If not, there's nothing I can do about it but 
> ignore it.

Then use the gnulib module ignore-value, and write
 ignore_value(system(fil_name))
if you really are absolutely positive that there is no reasonable
message to write to stderr if the command fails to run.  But I suspect
that you really have a bug, and SHOULD be caring about system() failure.

> Consequently, the value is valueless.  If the compiler is going to complain 
> about
> an unused value when that value is explicitly cast to void, then it is a 
> compiler bug.

Yes, we (aka gnulib writers) have argued that point with gcc developers
in the past, and the gcc guys argued back that the GNU Coding Standards
discourage cast-to-void and so their choice to not treat cast-to-void as
a way to silence __warn_unused_result is intentional.  At the end of the
day, we agreed to disagree, and instead wrote the gnulib ignore-value
module, which is guaranteed to work around compiler stupidity even when
cast-to-void won't.

Besides, ignore_value(system(foo)) does look better than
(void)system(foo), even if it is slightly more typing, and doesn't
violate the GNU Coding Standards recommendation.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]