-
Notifications
You must be signed in to change notification settings - Fork 13
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
Fix invalid memory write segmentation fault #118
Conversation
Solves (for the moment) #98 |
This code is kind of terrifying and doesn't look like the right way to cast an array to numpy. |
Do you have any examples of proper implementations? Was the original code correct? |
The "kind of terrifying" got my attention, so I stared at this PR for a while. I don't see how calling realloc on a NumPy array's pointer is ever the right thing to do. (Probably what @henryiii meant by "terrifying".) The More fundamentally, But it was allocated with length and you need it to have length How it worked at all—before or after this fix—was probably some accident of how memory was laid out in RAM. |
Okay, now I think I understand what's the problem. Actually, my first attempt at fixing this was making the allocation |
In fact, there is a commit named allocate len+1 instead of len |
Then your first thought was right and the test failed for something downstream. Replacing Either
The filling code seems to think that offsets needs to be There's an off-by-one error here somewhere; it depends on what |
Inspired by this PR: #124. |
Thanks! This clears things up a lot for me. I will try to put a fixing PR together for this tomorrow. |
This proposed change fixes the segmentation fault that occurs for some input arrays. My investigation indicated that the invalid memory access was happening in the last iteration of this loop. Specifically, the last element of
*ptroff
seemed to be after the allocated block. After running valgrind on this, I got messages like:I am still not 100% sure why the allocation is missing 1 byte. Maybe some trailing character is appended by pybind11 arrays? More knowledgable people might know. For the moment being, I decided to reallocate 1 more byte for all definitions of
*ptroff
. Tests are passing and the behavior seems normal thus far.