[BusyBox] Issue with strings (1.00-pre10)
Larry Doolittle
ldoolitt at recycle.lbl.gov
Thu Apr 15 23:25:00 UTC 2004
Guys -
> - why is the xmalloc is for "n+1"
strings.c wants to hold the first n qualifying letters of
each string in a buffer. This buffer needs to have n+1 bytes
allocated, so there is space for a trailing '\0'. This '\0'
makes it easy to print out those n bytes with
printf("%s", string);
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]);
> (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 does the error appear on every line, and not just the first?
Because the memory is re-used starting at the beginning for
every (potential) string. Paraphrasing (in the OTBS):
do {
c=fgetc(file);
if(ISSTR(c)) {
/* complications that make the next two lines conditional */
string[i]=c;
i++;
} else {
i=0;
}
} while(c!=EOF);
> Does xmalloc memset/bzero the malloc'd mem? If not, then that is
> likely your problem.
It does not.
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.
- Larry
More information about the busybox
mailing list