-
Notifications
You must be signed in to change notification settings - Fork 858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mi_rezalloc() zero initialization is unsafe #63
Comments
Ah this is a bit tricky: I now designed the zero interface, like A change we could make is to zero out always up to the useable size instead of requested size. I think that would be a good idea. _It seems to me that the rezalloc feature requires to store the requested size or at least to zero out the implicit allocated range form requested size to allocated size in all allocations. _ It does do that already, zero out any additional space if reallocating to a larger area. Of course, the existing data should not zero out. |
It's tricky indeed! Unfortunately, a change limited to the 'z' and 'c' allocation variants will not solve the problem. An example: Initial allocation of 6 bytes by The application copies its C string of length 5 plus a null byte as termination, i.e. it defines the state of the requested memory completely: Another part of the application wants to append a sixth character to the C string and ensure termination by using Can the implementation of Note that even if the old block is copied to a larger new block, the undefined memory area is copied as well, since only the block size is known. The extension of the zero initialization by one word in the area of the old block is overwritten, so that the extended initialization remains ineffective! (see master commit 7b4f359; alloc.c; lines 336 and 339) On possible solution without performance losses if the feature is not used would be the requirement to pass the originally requested size resp. count in |
Ah, nice example. The way it should be I think is that (a) |
> ...and perhaps we should do away with the rezalloc variantcs... |
removed rezalloc and variants in the latest |
In _mi_realloc_zero() and _mi_heap_malloc_zero() only the requested size and not the possibly larger allocated size is initialized to zero.
This may lead to uninitialized 'new' memory from the applications point of view in a following call to _mi_realloc_zero(), where the 'fits' test using mi_usable_size(p) and the memcpy(newp, p, ...) relies on a defined state of the previously allocated and not only the previously requested memory range.
Also take into account, that using mi_rezalloc() on a pointer from a call to mi_malloc() is legal but would lead to uninitialized memory in the range from the requested size to the allocated size too. It seems to me that the rezalloc feature requires to store the requested size or at least to zero out the implicit allocated range form requested size to allocated size in all allocations.
I wonder if this is why posix doesn't specify rezalloc().
On my windows machine this code sample can reproduce the problem:
The text was updated successfully, but these errors were encountered: