Skip to content

TextUnformatted implementation does not match expected behavior for its interface #3615

@Xeverous

Description

@Xeverous

Version/Branch of Dear ImGui:

Version: 1.80 WIP (17906)
Branch: master (71cc636)

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.

const std::string_view text = /* ... */;
ImGui::TextUnformatted(text.data(), text.data() + text.size());

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.

void TextUnformatted(const char* text, const char* text_end);

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 == nullptr but this should be irrelevant - what is relevant here is that begin == end and end is not dereferencable, therefore ptr should 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions