News for hackers. Stuff that matters.
Re:What If I Don't Actually Like My Users? (2008) by kazinator on Thursday June 02, 2016 @08:23PM (#11827430)
gets can be fixed by specifying some constant like GETS_MAX in some header file such as <limits.h>, along with the promise that gets doesn't write any more than GETS_MAX characters, including the null terminator, to its target buffer, and that the null terminator is always included. Then programs can just include the header and declare arrays of at least that size if they pass them to gets. Presto; no overflow.

I heard this idea was toyed with by the ISO C committee. Evidently, D. Gwyn proposed BUFSIZ to be re-used for this purpose.

Re:What If I Don't Actually Like My Users? (2008) by JoshTriplett on Thursday June 02, 2016 @09:25PM (#11827677)
If you're going to modify the code anyway, to include the header and set the buffer size, why not just use fgets, which takes a buffer size?

Current versions of glibc actually mark gets as deprecated (so you'll get a warning if you use it), and don't provide it at all if building for C11 or newer.

Re:What If I Don't Actually Like My Users? (2008) by kazinator on Friday June 03, 2016 @12:39AM (#11828344)
Good questions.

Since I don't use gets, I have to imagine myself fixing some legacy code.

Firstly, I imagine that this code already defines the arrays, so all I have to do is edit the definition to use this use this fictitious GETS_MAX constant, for which I have to include one simple header (which might already be included for something else). No actual logic has changed. (On the other hand, the adjustment to the size of the array to GETS_MAX could be a problematic issue; it is not without risk of something breaking.)

To use fgets, I have to edit the calls to add the size parameter. Moreover, I have to scan the buffer for the presence of a newline character and overwrite it with a null. gets does that and the code may be relying on it (using "relying" ironically, with regard to gets).