Fix crash in nk_buffer_realloc and forbid the use of realloc#878
Open
sleeptightAnsiC wants to merge 1 commit intoImmediate-Mode-UI:masterfrom
Open
Fix crash in nk_buffer_realloc and forbid the use of realloc#878sleeptightAnsiC wants to merge 1 commit intoImmediate-Mode-UI:masterfrom
sleeptightAnsiC wants to merge 1 commit intoImmediate-Mode-UI:masterfrom
Conversation
sleeptightAnsiC
commented
Jan 27, 2026
sleeptightAnsiC
commented
Jan 27, 2026
c1fa8e1 to
d5f2d34
Compare
RobLoach
reviewed
Feb 7, 2026
d5f2d34 to
efefccd
Compare
This comment was marked as outdated.
This comment was marked as outdated.
sleeptightAnsiC
added a commit
to sleeptightAnsiC/Nuklear
that referenced
this pull request
Mar 11, 2026
In short: the default implementation of nk_allocator (aka nk_malloc) is fundamentally broken, and should have used realloc instead of malloc. This led to the strange workaround in nk_buffer_realloc that would crash whenever you use custom allocator with correct realloc assumption. We cannot change nk_malloc at this point, as people could have implemented their own allocators based on it, but we can ensure that Nuklear itself will never try to reallocate memory. Additionally, I removed the assert checking for alloc failure, because Nuklear never do this for allocations; and also added a different one that checks agains shrink, because this code never suported such case, so it's better to catch it than to corrupt memory. Fixes: Immediate-Mode-UI#768 Ref: Immediate-Mode-UI#878 (comment) Ref: Immediate-Mode-UI#878 (comment)
efefccd to
c43b6ab
Compare
Contributor
Author
|
@RobLoach any chance for getting this reviewed again? Fixed all the points from my previous comments, and removed the changelog entry (because it's about to be removed in #909) I've decided to patch every suspicious line in that function. If |
sleeptightAnsiC
commented
Mar 12, 2026
In short: the default implementation of nk_allocator (aka nk_malloc) is fundamentally broken, and should have used realloc instead of malloc. This led to the strange workaround in nk_buffer_realloc that would crash whenever you use custom allocator with correct realloc assumption. We cannot change nk_malloc at this point, as people could have implemented their own allocators based on it, but we can ensure that Nuklear's internal code will never try to reallocate memory. Fixes: Immediate-Mode-UI#768 Reported-by: David Delassus <david.jose.delassus@gmail.com>
c43b6ab to
d02f6cb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #768
Please read the linked issue for explanation.
The default implementation of nk_allocator (aka nk_malloc) is fundamentally broken, and should have used realloc instead of malloc in the first place. This must have led to the workaround in nk_buffer_realloc that would crash whenever you use an allocator with correct realloc assumption.
We cannot change nk_malloc at this point as people could have implemented their own allocators based on same assumptions, nor we cannot change the nk_allocator interface because it would be a breaking change (and because we may actually want to use realloc in a future) but we can ensure that Nuklear itself never tries to reallocate memory in its internal code (which is already happening anyway, this only enforces it)
This is kinda ugly, but it still leaves a room for making a breaking change later.