snprintf() confusion
Posted Feb 5, 2004 8:52 UTC (Thu) by
ThePythonicCow (subscriber, #11308)
Parent article:
snprintf() confusion
The biggest problem that this snprintf patch will fix is not noted
in the article above.
There were a couple hundred places in the kernel that would loop:
len += snprintf (buf+len, buflen-len, "...", ...);
accumulating the number of non-nul chars written into a buffer in 'len'.
Well, actually, unbeknownst to the coder, accumulating the
total
number of characters that could have been written, if the buffer were
big enough.
The 2nd snprintf parameter (buflen-len, here) would go negative,
when the total number of chars that could have been written into
the buffer exceeded the buffer length.
Since this 2nd parameter is passed as a type size_t (unsigned),
this meant that the next call to snprintf claimed that the available
buffer size was **huge**. A license to overrun buffers. Not something
you desire in your average kernel.
The patch going in will have any buffer size that is huge (high bit
set) be special cased as if it was zero, putting nothing more in the
buffer.
(
Log in to post comments)