LWN.net Logo

Students uncover dozens of Unix software flaws (News.com)

Students uncover dozens of Unix software flaws (News.com)

Posted Dec 17, 2004 18:17 UTC (Fri) by JoeBuck (subscriber, #2330)
In reply to: Students uncover dozens of Unix software flaws (News.com) by pjdc
Parent article: Students uncover dozens of Unix software flaws (News.com)

The strl* functions are not magic, though they can be useful in getting rid of one class of error (and replacing it with another, less severe, class of error: the silent truncation of long lines).

Reading a line into a fixed-sized buffer is the problem, and use of strl* functions to get rid of the buffer overrun is only a band-aid. You may think that you can assume that no valid line will be more than N characters, and later on break when you get N+1 characters. You can get subtle bugs because strings are truncated to different lengths in different places.

Read lines into dynamically-sized buffers instead, e.g. std::string for C++ programmers, appropriate library for C programmers. The GNU Readline library, for example, does not impose arbitrary limits on string length (as long as you have virtual memory).


(Log in to post comments)

Students uncover dozens of Unix software flaws (News.com)

Posted Dec 17, 2004 20:35 UTC (Fri) by sdalley (subscriber, #18550) [Link]

> Reading a line into a fixed-sized buffer is the problem,
> and use of strl* functions to get rid of the buffer overrun
> is only a band-aid.

Well, yes, but only if you use it rather mindlessly without checking the return value. If this compares greater than the len parameter then you know immediately that the result is truncated.

In the case where you expect meaningful strings to be relatively short and longer ones to be erroneous, strl* functions are ideal and it makes perfect sense to have fixed-sized buffers. In the case where strings of arbitrary length are meaningful, you obviously want a fully dynamic solution such as asprintf and friends - see http://www.mibsoftware.com/libmib/astring/ .

Students uncover dozens of Unix software flaws (News.com)

Posted Dec 17, 2004 22:50 UTC (Fri) by hppnq (subscriber, #14462) [Link]

Well, yes, but only if you use it rather mindlessly without checking the return value.

Exactly. Hence the band-aid.

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds