Fixed-size buffers in C (10)

7 Name: #!/usr/bin/anonymous : 2006-03-25 11:44 ID:JZAgZf8x

>>1
Then again, the GNU C library has things like asnprintf() and other thoroughly nonstandard thingamabobs. No strlen() chains there. I'll agree 100% that the strlen() chain is the more error prone part in your code than anything else.

Much of the time though you can make a sane guesstimate on how much buffers you need. I.e. something like snprintf(buf, sizeof buf, "cheese%04d.jpg", intvalue) where intvalue is between 0 and 9999; hardly need any more space than 15 bytes there. Add to this that there may not be dynamic memory allocation on some targets (like an operating system kernel before it's got a good grip on "where the RAMs at, yo").

snprintf(), strlcpy() and so forth are pretty good at keeping your hands clean of the exploit-of-the-day though, turning cases where your unforeseen consequences would've become openings for stack smashing into just harmless string truncation. (Though that may in turn lead to stuff like symlink race condition attacks and so forth.)

This thread has been closed. You cannot post in this thread any longer.