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

Strict-aliasing violation in FormatTextureIDForDebugDisplay() #7256

Closed
d235j opened this issue Jan 22, 2024 · 3 comments
Closed

Strict-aliasing violation in FormatTextureIDForDebugDisplay() #7256

d235j opened this issue Jan 22, 2024 · 3 comments
Labels

Comments

@d235j
Copy link

d235j commented Jan 22, 2024

Version/Branch of Dear ImGui:

Version around Jan 6 2024, no modifications

Back-ends:

n/a

Compiler, OS:

gcc 13.2.1 20230801 on Arch Linux x86_64

Full config/build information:

n/a (build warning)

Details:

The following warnings are produced:

/sources_ssd/scopehal-apps/src/imgui/imgui.cpp: In function ‘void FormatTextureIDForDebugDisplay(char*, int, ImTextureID)’:                 
/sources_ssd/scopehal-apps/src/imgui/imgui.cpp:20263:55: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]                            
20263 |         ImFormatString(buf, buf_size, "0x%p", (void*)*(intptr_t*)(void*)&tex_id);                                                                                           
      |                                                       ^~~~~~~~~~~~~~~~~~~~~~~~~                                                                                             
/sources_ssd/scopehal-apps/src/imgui/imgui.cpp:20265:50: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]                            
20265 |         ImFormatString(buf, buf_size, "0x%04X", *(int*)(void*)&tex_id);                                                                                                     
      |                                                  ^~~~~~~~~~~~~~~~~~~~

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

@ocornut
Copy link
Owner

ocornut commented Jan 24, 2024

Thank you David, I think this should now be fixed by 81e0be8.
I added -fstrict-aliasing to some of my building/test scripts too.

@ocornut ocornut closed this as completed Jan 24, 2024
@d235j
Copy link
Author

d235j commented Jan 24, 2024

Note that using unions for type-punning is valid C but not valid C++ (though GNU compilers avoid undefined behavior here). In C++ you have a few options, the easiest being std::bit_cast but this is only introduced in C++20.
I suspect this would only be an issue on odd compilers (which is unfortunately common in gamedev) or with newer optimizations. I’m not sure if there’s a warning that would flag this.

More information:
https://tttapa.github.io/Pages/Programming/Cpp/Practices/type-punning.html
https://stbuehler.de/blog/article/2014/05/25/type_punning_with_unions.html

@ocornut
Copy link
Owner

ocornut commented Jan 24, 2024

We are using both unions and memcpy here, so I'm not sure but only skimmed through articles.
Thanks for reference! I guess we'll leave this simmering and see if anyone has any problem.

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

No branches or pull requests

2 participants