« New Tool: fastzolver | Main | Looking for reasonable corporate IM solution »
September 26, 2005
On being overly cautious
Most of us have least heard of buffer overflows, and all C programmers ought to pay attention to the size of target arrays and never attempt to put more in them than they have capacity for. Using something like:
strcpy(targetbuf, ptr); // might be dangerous
is — in general — asking for trouble because one doesn't know whether the pointed-to string (ptr) will actuallyfit in the target space. So many developers use size-limited functions that insure there won't be any overflow.
One of these is snprintf, which performs a printf-like copy to a buffer, but the buffer's size is included:
snprintf(targetbuf, targetsize, "message %d %s", i, p); // safer!
and it guarantees that it won't write beyond that. It's a wise technique, but one can be overly cautious about it. This was seen in code today (multiple times):
snprintf(result_string, BUFSIZE-1, "%s", "U");
I'll leave it as an exercise to the reader to find better ways to handle this particular circumstance.
Posted by steve at September 26, 2005 09:42 AM
Trackback Pings
TrackBack URL for this entry:
http://www.unixwiz.net/mt/trackback/50
Comments
slprintf ;)
Posted by: omerm at October 8, 2005 09:11 AM