bug-gnulib
[Top][All Lists]
Advanced

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

Re: printing pid_t values


From: Bruno Haible
Subject: Re: printing pid_t values
Date: Wed, 06 Jan 2021 19:00:13 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-197-generic; KDE/5.18.0; x86_64; ; )

Bernhard Voelker wrote:
> While pid_t is typedef'd to int on GNU/Linux platforms, it is a long on 
> Solaris 11:
> 
>   $ uname -a
>   SunOS gcc-solaris11 5.11 11.3 sun4u sparc SUNW,SPARC-Enterprise
> 
>   $ echo '#include <sys/types.h>' | gcc -E - | grep -w pid_t
>   typedef long pid_t;
> 
> There, one would need the %ld format when printing a pid_t's value.

Indeed:

$ cat foo.c
#include <stdio.h>
#include <unistd.h>
pid_t x;
int main()
{
  x = getpid();
  printf ("%d\n", x);
}
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:7:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 
has type ‘pid_t {aka long int}’ [-Wformat=]
   printf ("%d\n", x);
           ^

> Checking in gnulib, I only see it handled as %u or %d.
> Do we have an issue here?

I don't think there is an issue, because
  * The pid_t values occurring on Solaris are in the 5-digit range
    (> 0 and likely < 32768).
  * On 32-bit platforms, 'int' and 'long' are of the same size, and on
    x86_64 and sparc64 the parameter passing conventions in varargs
    say that such a value is passed zero-extended or sign-extended,
    and which of the two is irrelevant here.

But if you want to get rid of the warnings, you are welcome to submit
a patch that adds PRIdPID or PRIuPID to <inttypes.h> and uses it where
it makes sense. The alternative, adding casts to 'long' in many places,
is not so welcome because casts tend to hide bugs in future changes,
and should therefore be reserved to the cases where the format string
is passed through gettext(). (gettext() supports the POSIX-defined PRI*
macros but not ones that we invent on our own.)

Bruno




reply via email to

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