[BusyBox] Issue with strings (1.00-pre10)

Larry Doolittle ldoolitt at recycle.lbl.gov
Fri Apr 16 15:10:21 UTC 2004


> > If there were a compelling interest to avoid that trailing '\0',
> > you could replace the printf with
> >       for (j=0; j<n; j++) putchar(string[j]);
> This maybe would waste space:
> one more variable, one more for loop.

Evidently the author agrees.  If this were a small statically
linked program, the for loop could be _much_ smaller if it
could avoid pulling in copy of printf.

> > > (when we do an n-=1 two lines down)?
> > A possibly misguided attempt to save time and/or space
> > when comparing i against n in the body of the loop.
> Why misguided?
> maybe it could be done more elegantly .....

I said _possibly_ misguided.  n now _means_ different things
above and below that line.  Not a problem for the computer,
once everything is debugged.  But a potential maintenance problem
for _humans_, who have an extra complication to keep track of.

> >      string[n]='\0';
> > should definitely not be commented out.  That is what sets
> > byte n+1 (remember, the first byte is string[0]) to zero to
> > terminate the string that gets passed to printf.
> That's my fault, so let us put this in again.
> I'm just wondering why this works on my box....... 

Your malloc() is apparently kind enough to leave a nul byte
at the end of the allocated segment of memory.  This could
happen on purpose, to mitigate exactly this category of bugs.
If it happens by accident, that accident will depend strongly
on things like the endian-ness of the system.
 
     - Larry



More information about the busybox mailing list