Shock and Awe

From The Hacker's Guide To GPSD:

The best way to avoid having dynamic-memory allocation problems is not to use malloc/free at all.

An interesting approach to avoiding dynamic memory problems which in general are not that hard to debug with good tools (such as Valgrind), especially in relatively simple pieces of code like gpsd. However, in the next section:

The parsing of GPGSV sentences in the NMEA driver has been a persistent and nasty trouble spot, causing more buffer overruns and weird secondary damage than all the rest of the code put together.

Personally, I often find that not allocating buffers on the stack and using dynamically allocated memory with length-bounded functions helps fix buffer overruns.

21:58 Tuesday, 12 Jun 2007 [#] [computers] (5 comments)

Posted by iain at Tue Jun 12 23:55:14 2007:
Debugging with a shotgun!
Posted by James Henstridge at Wed Jun 13 03:04:45 2007:
To be fair, if you can sensibly implement a program without dynamic allocations, it is probably worth doing so.

Length-bounded IO and string manipulation functions work equally well on static buffers.  That said, if you are running into buffer overflows, it might indicate that fixed size buffers are not appropriate for the task ...

Perhaps they would have been better off isolating the driver code or writing it in a language with better string handling (they are already using Python, so that could be a possibility ...).
Posted by Benjamin Otte at Wed Jun 13 07:26:36 2007:
James, I have to disagree with you there. Static and dynamic buffers require a different mentality. And if people do both, they mix them up more often than not and either forget the limited length or freeing the data.
Using static data for me is strictly an afterthought for optimizations.

Now what did I came here to say actually? Ah right. Using static buffers is a thing that people learn by looking at "great code". And by that, they more often than not mean highly optimized code or libc. We all now glib isn't suitable for daemons for just this reason...
Posted by sxpert at Wed Jun 13 08:01:43 2007:
heh, what stupid reasons for coding like a pig would you expect from ESR...

shotgun debugging at it's finest
Posted by Jon at Wed Jun 13 11:29:03 2007:
I've heard stories of some software projects being developed without any dynamic memory on purpose: life-critical machinery etc.

Name:


E-mail:


URL:


Add 4 and 1 (required):


Comment: