>>>>> On Thu, 22 Nov 2012 15:16:54 +0100, Bastian Friedrich said:
> 
> Hi,
> 
> Am Donnerstag, 22. November 2012, 12:30:38 schrieb Martin Simmons:
> > >>>>> On Thu, 22 Nov 2012 10:22:39 +0100, Bastian Friedrich said:
> > > +             bsnprintf(add, sizeof(add), "%lu", (uint32_t)getpid());
> > 
> > Using %lu for uint32_t is wrong on 64-bit systems.  Why not cast to unsigned
> > long?
> 
> a) the respective statement does not result in any warnings or errors on my
>    (64 bit linux) machine

You get no warnings because gcc doesn't know about the format string in
bsnprintf.

This code should fail though:

#include <stdio.h>
#include <inttypes.h>

long fool(long x, long y) { return x+y; }
int fooi(int x, int y) { return x+y; }

int main()
{
  printf("%x %x %x %x %x %x %x %x %lu\n", 1,2,3,4,5,6,7,8,fool(0x100000001L,2));
  printf("%x %x %x %x %x %x %x %x %lu\n", 1,2,3,4,5,6,7,8,(uint32_t)fooi(1,2));
  printf("%x %x %x %x %x %x %x %x %lu\n", 1,2,3,4,5,6,7,8,(unsigned 
long)fooi(1,2));
  return 0;
}

There should be a 3 at the end of the last two lines, but I get 4294967299
(0x100000003) because the call in the first line has left something behind on
the stack.


> b) The code does just the same as line 632 in the same file, which says:
>    bsnprintf(s + strlen(s), ss, "%lu", (uint32_t)getpid());
>    so if either is fixed, so should be the other one.

Agreed.


> c) pid_t is signed, so the "unsigned" is probably wrong in any case ...

True, but I think that is just so that functions like fork can return -1 for
failure.  I've never seen a pid that is negative.


> If one wants to modify the patch, probably the straight forward version will 
> do:
> bsnprintf(add, sizeof(add), "%d", getpid());

bsnprintf(add, sizeof(add), "%d", (int)getpid());; would be better in case
pid_t is smaller than int.


> I don't know any system where size of pid_t exceeds that of int ...?

Me neither.

__Martin

------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Bacula-devel mailing list
Bacula-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-devel

Reply via email to