-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
Found by address sanitizer: global-buffer-overflow triggered from ImGui::InputTextWithHint() #8368
Comments
Thanks for the report and thorough investigation. I can confirm that there is a bug. const char* ImStrbol(const char* buf_mid_line, const char* buf_begin) // find beginning-of-line
{
IM_ASSERT(buf_mid_line >= buf_begin);
IM_ASSERT(buf_mid_line <= buf_begin + strlen(buf_begin)); The problem is that we lock if (is_displaying_hint)
{
buf_display = hint;
buf_display_end = hint + strlen(hint);
} Even though the cursor is not zero. What's particularly tricky about the situation is that is_displaying_hint is also needed early for this codepath: // Password pushes a temporary font with only a fallback glyph
if (is_password && !is_displaying_hint)
PushPasswordFont(); The native fix for the issue could make that a callback change from non-empty to empty could display a _Password field without the special |
…s the buffer contents in a way that alters hint visibility. (#8368)
Pushed a fix 5dd8408. Thanks again! |
Thanks for fixing! Background info: Your |
Version/Branch of Dear ImGui:
Version 1.91.7-docking
Back-ends:
imgui_impl_sdl2.cpp + imgui_impl_opengl3.cpp
Compiler, OS:
Linux, gcc
Full config/build information:
Details:
My best guess is that back-end, compiler and OS don't matter.
But to most easily demonstrate the bug it's best to use gcc or clang with the address sanitizer enabled.
I can trigger the bug with the example application (e.g. git/imgui/examples/example_sdl2_opengl3, git revision: v1.91.7-docking), with these two source code changes (I've also included a patch below):
ImGui::InputText(...)
toImGui::InputTextWithHint(..., "Enter command", ...)
.Makefile
to also include-fsanitize=address,undefined
inCXXFLAGS
.Then re-compile and start the example application:
cursor-up
to recall the (long) command.Normally this should show the previously entered (long) command. But instead it triggers an address-sanitizer crash, see below for the full crash-report.
I've done some preliminary investigation. Here's what I think is happening:
imgui.cpp:2071
inImStrbol(char const* buf_mid_line, char const* buf_begin)
on the statementbuf_mid_line[-1]
.buf_begin
parameter is valid and points to the string literal"Enter command"
.buf_mid_line
parameters points too far pastbuf_begin
.imgui_widgets.cpp:3871
this parameter is calculated asconst char* cursor_ptr = ... text_begin + state->Stb->cursor ...
, withStb->cursor
a value larger than the length of the"Enter command"
string literal.I hope that's sufficient info. Thanks in advance for investigating this.
Background info:
I think this is a real bug, but in practice this will very often appear to work just fine, without using address-sanitizer. Often the hint is a string-literal, and often the compiler groups many string literals together in the executable. So reading past the end of a string-literal does often not trigger a crash (you just read data from an adjacent literal). I discovered this bug by accident while using address-sanitizer to debug a problem in my code. (Un)fortunately I was not able to do that because this bug in Dear ImGui triggered before my bug had a chance :-)
Here's the full patch:
Here's the address-sanitizer crash report (without colors):
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
See above, can be reproduced with the example application with minimal code changes, and compiled with address-sanitizer enabled.
The text was updated successfully, but these errors were encountered: