Skip to content
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

UiBuffer::scratch_txt_two() returns the first pointer to a potentially invalid memory #762

Open
salvatorebenedetto opened this issue Jan 22, 2024 · 0 comments

Comments

@salvatorebenedetto
Copy link

salvatorebenedetto commented Jan 22, 2024

Describe the bug
UiBuffer::scratch_txt_two() calls push() twice and returns 2 pointers. The problem is that the first pointer is pointing to the old buffer when the second push causes a reallocation due to extend. When building in debug mode on MacOS, rustc 1.75, the old pointer, when there is a resize, will point to the old memory which it seems to be zero out (didn't look into the extend implementation) and so the string returned will point to a \0, resulting to a null string. The string is later used as label, which will cause imgui to assert because IDs can't be null.

This is because the initial capacity, at least on the system I've tried, is 0 and it is doubled everytime. Now, when using UiBuffer::scratch_txt_two() if the second txt does not fit into the vector, extend will trigger a reallocation and hold pointer will point to the old memory, which if zero out (like in my case), it will be cause a crash.

To Reproduce
This is reproducible in debug mode. Use the code in #725

Expected behavior
Library shouldn't return an invalid pointer.

Screenshots

Please describe your environment

  • imgui-rs 0.11
  • MacOS 14.2.1

Other information
One solution I've found is to instead of allocation the buffer with new() to call with_capacity(max_len) but that requires the method not to be const, which I don't know if it is an acceptable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant