-
Notifications
You must be signed in to change notification settings - Fork 41
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
Result of zpl_alloc() is often not checked #104
Comments
If you can provide a PR for this one that would be great! Thanks again for all the reports, I appreciate the help! |
@zpl-zak - a significant trickiness to the change is the problem occurs a lot in I'm minded to suggest I add I didn't anticipate causing that much of a change! I am very open to any suggestions from you regarding which direction I set of in. |
Sadly that would be difficult to change as many other modules rely on an allocation that would be affected. However, since we know this issue is related to containers (zpl_array, zpl_buffer), I suggest we expand the header structure of these containers and add an error code field that would get set in case we fail any allocation. Users can then refer to this field to check for OOM errors and handle them appropriately, which is a viable alternative to returning by error code. The user, in this case, would be the parser/adt code that would then validate this error code and return the appropriate error to the library user. EDIT: I can address this tomorrow, and I will use your demo code as a test case to validate the bad path in our UTs. |
I'm not sure the knock-on effect is too bad. I guess if zpl ends up checking NULL everywhere, each module is getting updated one way or another. Neither approach requires that to happen today, I was just trying to leave a pattern that could be used each time later on. Here is the proposed change master...rheatley-pervasid:zpl:add-alloc-call I couldn't see a good reason why the array held onto the original pointer value, so removed it for simplicity. (There is an unnecessary fix to |
I see, yes this approach would work well in that case. I agree performing the alloc in the macro is flawed due to no error handling being present, luckily with your approach, it shouldn't break backward compatibility indeed. Let's continue going down this path. |
I think arrays are complete Because I cannot rely on sizeof() for the array type, elem_count has got cached. If you are still happy with this direction I will progress it over the next week. |
Looks good to me; thanks again for the contribution! 👍🏻 |
Been a lot busier than anticipated, so a bit delayed finishing off the arrays work. I came to the conclusion that API pattern wouldn't work well when the array got reallocated underneath us. So I've had a second go, master...rheatley-pervasid:zpl:alloc-take-2 p.s. there is at least a bug in |
I think this approach will work out fine in the end. I like how things look so far, thank you again! |
Hi @zpl-zak - been quite busy so this got a bit neglected. Would you like anything tidying up or I'll create a PR? json.c looks to have had a lot of whitespace in, which my editor stripped. I can restore it if you want a more minimal diff. |
Hi @rheatley-pervasid, I will get back to it this upcoming weekend. Sorry for the delay! |
@rheatley-pervasid Sorry again for disappearing! I think it looks fine, could we also expand it to zpl_buffer and other macro-based collections? If not that's fine too, we can go ahead with PR and I can follow your changes and apply them to other collections. Thanks for the contrib! |
@zpl-zak no worries, it seemed optimistic to look at it much over Christmas :)
|
Hi, I'm sorry I am no longer current on this issue. Are we good to close it now, or are more actions required? |
Hi @zpl-zak - it is up to you really. (I was fixing some more stuff here, https://github.com/rheatley-pervasid/zpl/commits/alloc-improvements - but I've been very short of time recently, so I doubt I will make any more progress in the short term) |
Thanks for the response! I will look into the mentioned branch, apply the fixes to the main repo, and continue down this path to cover more locations. Thank you for the help so far. No worries, it does not hurry. I'll keep this issue open for tracking purposes. |
Sorry to bombard you with all the issues! It's honestly because I am enjoying using the library so much for all sorts of projects :)
The following code seg faults
This is because zpl_alloc fails in zpl_array_init_reserve (from zpl_array_init, from zpl_adt_make_branch)
As you can see
zpl__ah
is used without a null check.There seem to be quite a few instances where the failure is not checked. I'm hoping you agree it should return an error rather than segmentation fault!
I'm happy to try and fix the related issues and submit a PR if you like.
Let me know your thoughts.
(In my use case I am on an embedded device so prefer static allocation, hence zpl_alloc can fail without the heap being exhausted)
The text was updated successfully, but these errors were encountered: