Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 1 Feb 2015 21:11:25 +0800
From:      Erich Dollansky <erichsfreebsdlist@alogt.com>
To:        Paul Koch <paul.koch@akips.com>
Cc:        freebsd-stable@freebsd.org
Subject:   Re: top, fixed buffer length in utils.c
Message-ID:  <20150201211125.2fb21c39@B85M-HD3-0.alogt.com>
In-Reply-To: <20150201195722.68845794@akips.com>
References:  <20150201175159.7fa88d16@B85M-HD3-0.alogt.com> <20150201195722.68845794@akips.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

On Sun, 1 Feb 2015 19:57:22 +1000
Paul Koch <paul.koch@akips.com> wrote:

> On Sun, 1 Feb 2015 17:51:59 +0800
> Erich Dollansky <erichsfreebsdlist@alogt.com> wrote:
> 
> > Hi,
> > 
> > I came across this here in utils.c which is part of top:
> > 
> > 
> > /*
> >  * How do we know that 16 will suffice?
> >  * Because the biggest number that we
> >   will
> >  * ever convert will be 2^32-1, which
> >  is 10
> >  * digits.
> > */
> > 
> > char *itoa(val)
> > 
> > register int val;
> > 
> > int can be 64 bits on a amd64 machine. Why is the author of this
> > code so sure that we will never cross the 32 bit boundary?
> > 
> > Erich
> 
> I thought an 'int' was a 32bit number on amd64 arch.

the problem is that top is not a x86 only program but should work on
all platforms. Leaving this like it is might will cause problems when
int becomes more than 32 bits wide.

Of course, if it is known that the value to be converted will always be
within 32 bits, it is not a problem to do it as it is done.

> 
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> int
> main (void)
> {
>    printf ("%zd\n", sizeof (int));
>    exit (0);
> }
> 
Yes, this is the reason why it works on 32 bit platforms and also on
amd64.

Erich



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20150201211125.2fb21c39>