dbTalk Databases Forums  

cast pid_t to int when using *printf

comp.databases.postgresql.patches comp.databases.postgresql.patches


Discuss cast pid_t to int when using *printf in the comp.databases.postgresql.patches forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Oliver Jowett
 
Posts: n/a

Default cast pid_t to int when using *printf - 09-24-2004 , 01:51 AM






gcc (3.2.3 on Solaris 9) warns about a couple of places where a pid_t is
formatted with %d by a printf-family function. This patch explicitly
casts to int to suppress the warning.

-O

Index: src/backend/postmaster/pgstat.c
================================================== =================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/pgstat.c,v
retrieving revision 1.80
diff -u -c -r1.80 pgstat.c
*** src/backend/postmaster/pgstat.c 29 Aug 2004 05:06:46 -0000 1.80
--- src/backend/postmaster/pgstat.c 24 Sep 2004 06:46:27 -0000
***************
*** 1505,1511 ****
snprintf(pgStat_fname, MAXPGPATH, PGSTAT_STAT_FILENAME, DataDir);
/* tmpfname need only be set correctly in this process */
snprintf(pgStat_tmpfname, MAXPGPATH, PGSTAT_STAT_TMPFILE,
! DataDir, getpid());

/*
* Arrange to write the initial status file right away
--- 1505,1511 ----
snprintf(pgStat_fname, MAXPGPATH, PGSTAT_STAT_FILENAME, DataDir);
/* tmpfname need only be set correctly in this process */
snprintf(pgStat_tmpfname, MAXPGPATH, PGSTAT_STAT_TMPFILE,
! DataDir, (int)getpid());

/*
* Arrange to write the initial status file right away
Index: src/backend/postmaster/postmaster.c
================================================== =================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.425
diff -u -c -r1.425 postmaster.c
*** src/backend/postmaster/postmaster.c 9 Sep 2004 00:59:33 -0000 1.425
--- src/backend/postmaster/postmaster.c 24 Sep 2004 06:46:27 -0000
***************
*** 2835,2841 ****
*/
ereport(DEBUG3,
(errmsg_internal("%s child[%d]: starting with (",
! progname, getpid())));
for (i = 0; i < ac; ++i)
ereport(DEBUG3,
(errmsg_internal("\t%s", av[i])));
--- 2835,2841 ----
*/
ereport(DEBUG3,
(errmsg_internal("%s child[%d]: starting with (",
! progname, (int)getpid())));
for (i = 0; i < ac; ++i)
ereport(DEBUG3,
(errmsg_internal("\t%s", av[i])));


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Neil Conway
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 02:00 AM






On Fri, 2004-09-24 at 16:51, Oliver Jowett wrote:
Quote:
gcc (3.2.3 on Solaris 9) warns about a couple of places where a pid_t is
formatted with %d by a printf-family function.
For curiosity's sake, what formatting escape does gcc prefer?

-Neil



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #3  
Old   
Oliver Jowett
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 02:34 AM



Neil Conway wrote:
Quote:
On Fri, 2004-09-24 at 16:51, Oliver Jowett wrote:

gcc (3.2.3 on Solaris 9) warns about a couple of places where a pid_t is
formatted with %d by a printf-family function.


For curiosity's sake, what formatting escape does gcc prefer?
I don't think there is an escape for pid_t, you always have to cast it.

-O

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #4  
Old   
Peter Eisentraut
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 03:56 AM



Am Freitag, 24. September 2004 09:34 schrieb Oliver Jowett:
Quote:
Neil Conway wrote:
On Fri, 2004-09-24 at 16:51, Oliver Jowett wrote:
gcc (3.2.3 on Solaris 9) warns about a couple of places where a pid_t is
formatted with %d by a printf-family function.

For curiosity's sake, what formatting escape does gcc prefer?

I don't think there is an escape for pid_t, you always have to cast it.
I think what he was asking is this: Since pid_t has to be a signed integer
type, but gcc does not accept %d for it, then it could be that pid_t is wider
than an int, so casting it to int would potentially lose information.

(Btw., the Windows port defines pid_t as unsigned long; that's surely wrong.)

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #5  
Old   
Magnus Hagander
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 04:06 AM



Quote:
(Btw., the Windows port defines pid_t as unsigned long;
that's surely wrong.)
In what way is that wrong? A PID on Windows is a DWORD, which is an
unsigned long. Or am I missing something (probably..)?

//Magnus

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #6  
Old   
Peter Eisentraut
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 04:30 AM



Am Freitag, 24. September 2004 11:06 schrieb Magnus Hagander:
Quote:
(Btw., the Windows port defines pid_t as unsigned long;
that's surely wrong.)

In what way is that wrong? A PID on Windows is a DWORD, which is an
unsigned long. Or am I missing something (probably..)?
The mingw header files define pid_t as int, so we shouldn't redefine it in the
first place. The rest of the POSIX world also assumes that pid_t is signed,
so you might break a bunch of interfaces if it's not. Note that this is
independent of the fact that the actual process identifiers are all positive,
both on Windows and on Unix systems.

(Tertiary note: Never #define one type to another, always use typedef.)

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #7  
Old   
Oliver Jowett
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 05:31 AM



Peter Eisentraut wrote:
Quote:
Am Freitag, 24. September 2004 09:34 schrieb Oliver Jowett:

Neil Conway wrote:

On Fri, 2004-09-24 at 16:51, Oliver Jowett wrote:

gcc (3.2.3 on Solaris 9) warns about a couple of places where a pid_t is
formatted with %d by a printf-family function.

For curiosity's sake, what formatting escape does gcc prefer?

I don't think there is an escape for pid_t, you always have to cast it.


I think what he was asking is this: Since pid_t has to be a signed integer
type, but gcc does not accept %d for it, then it could be that pid_t is wider
than an int, so casting it to int would potentially lose information.
pid_t on the Solaris/sparc system is a long (but both int and long are
32 bits). Some experimentation shows that gcc is happy with a %ld format
specifier. But compiling the same code on a Linux/x86 system makes gcc
complain when applying %ld to pid_t, so we will need a cast there either
way.

I notice that elog.c casts MyProcPid to long and uses %ld. Amusingly,
MyProcPid is explicitly an int..

-O

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #8  
Old   
Neil Conway
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 05:34 AM



On Fri, 2004-09-24 at 20:31, Oliver Jowett wrote:
Quote:
pid_t on the Solaris/sparc system is a long (but both int and long are
32 bits). Some experimentation shows that gcc is happy with a %ld format
specifier. But compiling the same code on a Linux/x86 system makes gcc
complain when applying %ld to pid_t, so we will need a cast there either
way.
I guess it would be safest to use %ld and cast pid_t to long. Of course,
this seems a little paranoid -- is there actually a system with
sizeof(pid_t) != 4?

-Neil



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #9  
Old   
Tom Lane
 
Posts: n/a

Default Re: cast pid_t to int when using *printf - 09-24-2004 , 09:13 AM



Neil Conway <neilc (AT) samurai (DOT) com> writes:
Quote:
I guess it would be safest to use %ld and cast pid_t to long. Of course,
this seems a little paranoid -- is there actually a system with
sizeof(pid_t) != 4?
Traditionally PIDs fit in 16 bits, let alone 32. I'd recommend that we
standardize on casting pid_t to int for printing purposes; I think
that's what's being done in more places than not. Also, as you note, we
are using int variables to hold PIDs in many places --- I don't think
it's worth running around and changing those either.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.