raygui: fix buffer overflow crash in TextBox when string is smaller than textSize - #547
Merged
gen2brain merged 1 commit intoJun 1, 2026
Conversation
shdynila
marked this pull request as ready for review
May 31, 2026 08:13
shdynila
force-pushed
the
fix-raygui-textbox-buffer-overflow
branch
from
May 31, 2026 16:56
92b103c to
2e580b0
Compare
shdynila
force-pushed
the
fix-raygui-textbox-buffer-overflow
branch
from
May 31, 2026 19:45
2e580b0 to
34bab32
Compare
gethiox
approved these changes
Jun 1, 2026
Owner
|
Thanks, I am merging this. Note that raygui in this state is contributed; before, there was a plain Go code that just called into the raylib bindings, mimicking what the C API did. But, maintaining and following upstream was an issue, so I accepted the CGo solution that worked for users. Perhaps, introducing that again in the age of AI, where it is not that difficult to follow, is again a good idea. |
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.
Description
Fixes a heap corruption/buffer overflow crash when calling
raygui.TextBoxwith a string that is shorter than the requestedtextSize(e.g., an empty string""with atextSizeof25).Root Cause
In Go,
bs := []byte(*text)creates a byte slice whose size is bound to the string's current length, rather than the requestedtextSize.If
len(bs)is smaller thantextSize, the C functionGuiTextBoxreceives a pointer to a small Go-allocated buffer but expects it to be at leasttextSizebytes. When typing a character,GuiTextBoxwrites out-of-bounds, corrupting the Go heap and causing access violations / segmentation faults.Solution
bsis smaller thantextSize, it is re-allocated to have a length of at leasttextSize(newBs := make([]byte, textSize); copy(newBs, bs)).C.CString): Instead of passing a Go garbage-collected slice pointer directly to C (&bs[0]), which violates cgo pointer-passing rules for mutable buffers, we now copy it safely to the C-heap usingC.CString(string(bs)).C.freeand successfully copy the updated string back into Go memory with*text = C.GoString(ctext)afterGuiTextBoxexecutes.