libsodium
libsodium copied to clipboard
MSVC: Add –d2UndefIntOverflow–
When using zig/clang/gcc, we unconditionally enable -fwrapv in order make overflowing signed operations well defined.
But this is not the case when using MSVC.
–d2UndefIntOverflow– is a compiler flag equivalent to -fwrapv: https://devblogs.microsoft.com/cppblog/new-code-optimizer/
We should figure out how to enable it in the Visual Studio solutions.
Does anybody know how to do this?
Well tried using latest VS 2022: Project properties -> C/C++ -> Command line -> added /d2UndefIntOverflow-
But on got compile error: Error C1007 unrecognized flag '-UndefIntOverflow-' in 'p2'
So it seems it's no longer valid flag, unless I'm missing something .
I bit the bullet and booted my dual boot machine into windows to do some experimentation this morning. Using the same method of adding the flag to Commandline like GPUex /d2UndefIntOverflow- does not work but /UndefIntOverflow- does. I assume it does the same thing. if I search for UndefIntOverflow in the msvc docs I get no results with or without d2. https://lectem.github.io/msvc/reverse-engineering/build/2019/01/21/MSVC-hidden-flags.html lists /UndefIntOverflow+ and /UndefIntOverflow- as hidden flags but not versions with d2 in them.
My understanding is that d2 indicates which stage of compilation the flag effects and as far as I can see it seems this is generally or always only used for hidden flags not meant for public consumption. So my guess is that they decided to make UndefIntOverflow publicly available at some point and renamed the internal flag but haven't finalized things and actually put the flag in the documentation yet (and that they may or may not have just forgotten about the whole thing).
So really old versions of visual studio prior to that devlog from 2016 likely don't support the option at all. The first version likely supports it with the d2 prefix and then at some point the prefix is dropped which is the state for the latest version.
@jedisct1 So as I see it we could:
- Choose not to bother with the flag until it is officially documented,
- Implement it without the d2 prefix for visual studio 2022 and future versions as they are created,
- Have someone install all supported versions of visual studio and document which flag works and add in the correct flag for each version or
- Similar to 3 but have someone determine when the d2 prefix was dropped and put the flag in for all versions that drop the prefix;
I recommend 1 or 2. I believe 2 would just be a minor change to the libsodium.vcxproj file:
...
<ClCompile>
<AdditionalOptions>/UndefIntOverflow- %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
...