[Python-Dev] How is obmalloc safe with "Invalid read of size 4" ?

Hrvoje Niksic hrvoje.niksic at avl.com
Tue Mar 24 16:22:31 CET 2015


On 03/24/2015 03:28 PM, Karl Pickett wrote:
> So we then tried running it under valgrind, and we got a lot of nasty
> errors.  Even after reading the Misc/README.valgrind, which talks about
> *uninitialized* reads being ok, I still don't see how reading from
> *freed* memory would ever be safe, and why the suppression file thinks
> thats ok:

PyObject_Free() is not reading *freed* memory, it is reading memory 
outside (right before) the allocated range. This is, of course, 
undefined behavior as far as C is concerned and an invalid read in the 
eyes of valgrind. Still, it's the kind of thing you can get away with if 
you are writing a heavily optimized allocator (and if your name starts 
with "Tim" and ends with "Peters").

README.valgrind explains in quite some detail why this is done. In 
short, it allows for a very fast check whether the memory passed to 
PyObject_Free() was originally allocated by system malloc or by Python's 
pool allocator.



More information about the Python-Dev mailing list