mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-08 19:02:56 +00:00
TextUnformatted implementation does not match expected behavior for its interface #3615
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#3615
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Version/Branch of Dear ImGui:
Version: 1.80 WIP (17906)
Branch: master (
71cc636696)Back-end/Renderer/Compiler/OS
Back-ends: https://github.com/mosra/magnum-integration/tree/master/src/Magnum/ImGuiIntegration
Compiler: all
Operating System: all
My Issue/Question:
I'm very surprised by implementation's behavior.
The code above crashes in some cases. Why? Because the implementation does not match what library user (me in this case) expects the library to do.
As someone who is working with idiomatic C++, this function takes a range. A range can be defined by an iterator + size or 2 iterators. Pointers are the simplest types that are iterators. There is an invariant that end iterator (which should be equal to begin + size) can not be dereferenced.
The problem: I'm passing a valid range. In my case, the string is empty and the range is (
ptr,ptr + 0).ptr == nullptrbut this should be irrelevant - what is relevant here is thatbegin == endandendis not dereferencable, thereforeptrshould not be dereferenced whatever it is. Entire STL works with empty ranges and so any other library may be expected to honor range idioms.I guess it will be very easy to fix. I just want to point out that not "honoring" range idiom here is very surprising considering how the interface looks. I was very pleased by seeing an interface that does not require null-terminated strings and it was very tempting to use it in some STL/boost algorithms with a lambda that calls Dear ImGui.
The problem is that accepting
TextUnformatted(0, 0)as a valid call will prevent a detecting a common error. I believe that may be more problematic.What common error do you have in mind?
0 (first),0 (size)or0 (first),0 (last)are both completely valid ranges and ... an empty non-null-terminated string is still a valid string. Any function/algorithm that traverses arrays is expected to work with empty ranges. Often no extra code needs to be written to handle empty ranges because they just immediately terminate all possible loops. In Dear ImGui's case it's only failing because the condition for null-terminated string is at the beginning which is not aware that the given string is a valid non-null-terminated string.Added support for this with
780c1ee2.Sorry for late reaction and thanks for the suggestion.
Also added basic tests for it.
They are valid when intended when passed explicitly as a range (two params), but consider that second parameter is defaulting to NULL:
When
SomeValue()return NULL, you'd expect standard text functions to helpfully crash on a NULL pointer as we are often doing for being a low-level library. Instead this is now treated as a NULL,NULL range and will silently display an empty line. It is debatable whether this is desirable.But simultaneously I already the desired support for ranges is nice but I opted for it.