Re: [sharutils-4.11.1] Compilation warnings

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.utils.bugs
Organization Red Hat, Inc.
Message-ID <[email protected]>
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
signature.asc (application/pgp-signature, 619 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQEcBAEBCAAGBQJQwTh6AAoJEKeha0olJ0NqzksIAIBpym7UL1MuCGD8AEO9QeJc
S0t+GATelfutxBxF8ZTUTtIQ5uCqrqHm4/xm+7+i4hPSnCEefNGd22fWr2GDQv2F
/jo+5RF2QT7lFXn+TM/BlyLf/YwvHtujMWFaS7ap3U/EtwaaJqORIzgLKLzJjpw5
Uup/0qhntkSWwa9zKM/XO79jdAROT92kuvQXT4EC0VLoov+oAQx9uoc4KubGrPRb
WGsN7E5bRhVoR6G5pA3qz2naq8fzlX1x6ijdexO9KzYqf7eg8vy5jd6SkjrshyO9
Lsd89zOcWaxIuSbYJXzPAhWRY8Z5xVcNzXCT4MXioy2jn3suU6OwjEGgLC9cqeA=
=q9DC
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.