On Mon, Mar 14, 2016 at 02:27:56PM -0400, Mike Frysinger wrote:
> On 14 Mar 2016 18:11, Bartosz Gołaszewski wrote:
> > 2016-03-14 15:27 GMT+01:00 Mike Frysinger <[email protected]>:
> > > On 14 Mar 2016 11:07, Bartosz Golaszewski wrote:
> > >> +#ifndef __BB_NAMESPACE_H
> > >> +#define __BB_NAMESPACE_H
> > >
> > > use a naming style like other busybox headers
> > >
> > >> +/*
> > >> + * Longest possible path to a procfs file used in namespace utils. Must 
> > >> be
> > >> + * able to contain the '/proc/' string, the '/ns/user' string which is 
> > >> the
> > >> + * longest namespace name and a 32-bit integer representing the process 
> > >> ID.
> > >> + */
> > >> +#define NS_PROC_PATH_MAX (sizeof("/proc//ns/user") + sizeof(pid_t) * 3)
> > >
> > > using the sizeof pid_t as a proxy for how many chars it'd take to render
> > > a decimal number in ASCII is wonky.  just hardcode it as "10" since that
> > > is the largest unsigned 32bit number ("4294967296").
> > 
> > Can you elaborate on how it's wonky? While your solution is perfectly
> > fine I think that there's nothing wrong with the way I've done it
> > neither.
> 
> the code seems to assume that the byte size scales into the number of
> chars required to represent the number in base 10 when it's really a
> log scale.  here's a table to show it's wonky:

sizeof is already a log scale. 3*sizeof(pid_t) is
3*log_256(MAX_PID+1), which is greater than 3*ceil(log_1000(MAX_PID)),
the actual space requirement.

> pid_t |sizeof |*3 val|actual|
> size  |(bytes)|(char)|      |
> (bits)|       |      |      |
> ------|-------|------|------|
>   8   |  1    |  3   |  4   |
>  16   |  2    |  6   |  6   |
>  32   |  4    | 12   | 11   |
>  64   |  8    | 24   | 20   |
> 128   | 16    | 48   | 40   |
> 
> the "actual" value is one higher than you might expect because pid_t
> is a signed quantity.  for 8bit, "-128" is 0x80 and takes 4 bytes.
> for 32bit ("int"), "-2147483648" is 0x80000000 and takes 11 bytes.

While pid_t is signed, negative values are not meaningful as pids;
they're pgids or special sentinels in some contexts. If this code is
trying to print a negative pid into a pathname used in /proc, that's a
bug. But in general, when using the 3*sizeof idiom you should add +1
for a sign if needed and another +1 for null termination if not
already included elsewhere.

Rich
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to