Don said:
Hi all,
our app crashes because it derefs null ptrs. guy who quit caused this.
boss want to see if we can fix it quick. can we malloc mem at addr 0 so
null ptrs dont' cause core dump? we need mallocat func like this
mallocat(size, addr)
then we call it mallocat(size,0) and no more crach. is there mallocat
func in linux c? plz let me know. thanks in adv...db
If your app dereferences null pointers, that's because it's lost track
of (or never knew in the first place) the location that the pointer
was supposed to point at. Until you fix that problem, your program
will work only by accident, and even if mallocat() were a possibility,
it wouldn't solve that problem.
Look at it this way: if you have a value of 3 stored in a given
location in memory, and the pointer is supposed to point at that
location, and there's a bug in the program that results in the pointer
being null instead of pointing at that location, how are you going to
retrieve the 3? Letting a null pointer reference work wouldn't help
even if it were possible, because the 3 will be somewhere else, not at
the location the null pointer points at.
Let's say that both the writing of the value and the reading of it are
both done to the same location you've allocated with a hypothetical
mallocat(). Does that solve your problem? Probably not. If there's one
such error in this code, there's probably more than one. If one part
of your code stores 42.0 in that location, then another part stores
"Help - I was programmed by an idiot!" in that same location, then a
third program tries to retrieve the 42.0, it's probably not going to
find it. It's going to reinterpret the bits representing that string
as of they represented a floating point number. If you're lucky, it
will interpret them as a signaling NaN which will cause your program
to fail before it does any more damage. If you're unlucky, it will
interpret it as an ordinary, large, negative number, and add that
amount to your companies bank balance.