-
Notifications
You must be signed in to change notification settings - Fork 112
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
Is sdallocx safe to call with a null pointer? #143
Comments
According to jemalloc's docs:
so IIUC the following should work: void* ptr = malloc(0); // Does this return NULL ? jemalloc docs don't say
sdallocx(ptr, 0); yet that would always make the void* ptr = malloc(size);
if (size == 0) { free(ptr); } else { sdallocx(ptr, size); } which IMO would make the API of jemalloc unnecessarily hard to use correctly. @davidtgoldblatt could you clarify what the intent is here? |
That is, jemalloc memory allocation functions only returns |
The behavior of calling We should probably also add an |
We've historically tried to avoid taking a strong stance on this sort of implementation-defined thing (e.g. return value of malloc(0)). Wanting C++ interop has made this harder and harder, to the point that I think we might just as well commit to malloc(0) being non-NULL (barring OOM). I'm not sure anyone ever thought too hard about the sdallocx-with-zero case. We used to assert against it, but took it away (again, for C++ interop reasons). The more I think about it, this less I'm convinced that it was ever the right thing to do. If we give out non-NULL pointers in response to a request for a particular size, we ought to accept that size back again in sdallocx. |
@davidtgoldblatt is there an issue in jemalloc upstream that we could cross-reference from here ? In Rust, the behavior of requesting an allocation with zero-size is undefined, i.e., |
I don't think so; what specifically are you referring to though? (I think a catch-all "there should be better documentation around edge-cases" issue with a list would be useful; I don't think we have one at the moment). |
I started a list at jemalloc/jemalloc#1716 |
Thanks, that was precisely what I was referring to. |
With recent changes to an application, I am running into the error
memory allocation of n bytes failed
(where n is a small number eg. 5, 1, 4) when usingjemallocator
. The application works fine when using the system allocator.Running
jemalloc
in debug mode reveals that it is failing the assert thatsdallocx
was not called with a null pointer.The
jemalloc-sys
docs imply it is safe to callsdallocx
with a null pointer (it states "no action occurs"). I cannot find anything injemalloc
docs to confirm this, and by the assert in the code it appears as if it is explicitly not allowed.Modifying
sdallocx
to simply return when called with a null pointer appears to fix the issue, but I am not sure of the ramifications of this change.I apologize for not having a minimal example with which to replicate. The scenario in which the error occurs is fairly involved.
The text was updated successfully, but these errors were encountered: