Re: sprintf and strings with NULL byte
| From: | rasmus@php.net | Date: | Wed, 25 Apr 2001 20:57:29 +0000 |
| Subject: | Re: sprintf and strings with NULL byte | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-52436@lists.php.net to get a copy of this message | ||
> Do we have any facilities (in C) for sprintf'ing the strings that may
> have NULL byte inside? For example, if name is "foo\0bar", and name_len
> is 7,
>
> sprintf(buf, "name: %*s", name_len, name);
>
> Does not give the correct result - stops at the first null byte.
Don't think so. And it also doesn't work if the format string contains a
null byte because the parsing loop does:
while (format[inpos]) { ...
I don't see any reason to not change this to loop over
(*args[0])->value.str.len instead here.
As for the %*s issue. We are passing in the length of the string arg in
the call:
php_sprintf_appendstring(&result, &outpos, &size,
(*args[argnum])->value.str.val,
width, precision, padding,
alignment,
(*args[argnum])->value.str.len,
0, expprec);
So we have the info. In php_sprintf_appendstring() the replacement is
done using:
strncpy(&(*buffer)[*pos], add, MIN(max_width, len)+1);
Looks like simply changing this to a memcpy() call will fix this. Again,
I don't see any reason not to make this change. PHP is supposed to be
8-bit clean and I think this should extend to the printf functions as
well.
-Rasmus