Skip to content

Make headers safe against <windows.h> min/max macros - #6

Merged
eyalamirmusic merged 2 commits into
mainfrom
orch/windows-minmax
Nov 17, 2026
Merged

eyalamirmusic merged 2 commits into
mainfrom
orch/windows-minmax

Conversation

@eyalamirmusic

@eyalamirmusic eyalamirmusic commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

What and why

<windows.h> without NOMINMAX defines min and max as function-like macros. A consumer that includes it before this library breaks on calls like std::numeric_limits<int>::max() in Span.h's Detail::fitsInt. PureDOOM ran into this in src/DOOM/Host/Host.cpp and worked around it with NOMINMAX. eacp only avoids it because its own WinInclude.h defines NOMINMAX. A header-only library shouldn't rely on its consumers doing that.

Fix (commit 1): I checked every header and test source for macro-vulnerable calls and put the name in parentheses, the standard way to stop the macro expanding:

  • (std::numeric_limits<T>::max)() / min: Span.h (fitsInt), Vector.h (size() assert), Value.h (getMin/getMax)
  • (std::min)(...) / (std::max)(...): StaticVector.h (x3), SmallVector.h, MultiPoolAllocator.h

No other <windows.h> macro collisions turned up (small, interface, near/far, ERROR, etc.). The full-<windows.h> test below would catch any.

Regression test (commit 2): Tests/Windows/WindowsMinMaxTests.cpp, added to the NanoTest target only under if(WIN32). It explicitly #undefs NOMINMAX, includes full <windows.h> first, and #errors if max isn't defined. It then includes the umbrella header plus every public header individually, and registers Windows.headers_compile_without_NOMINMAX, which uses ValueWrapper<int>::getMin/getMax and StaticVector::resize/getLastValidElementIndex.

Validation

Box Commands Result
win (MSVC 14.51, x64, Ninja) rjob ... -VsDev -- cmake -S . -B build -G Ninja && cmake --build build, then ctest --test-dir build --output-on-failure -j16 Build OK, 564/564 passed, including Windows.headers_compile_without_NOMINMAX
win, negative check git checkout 9f26b4a -- ea_data_structures (headers before the fix), rebuild Fails as expected: WindowsMinMaxTests.cpp.obj hits C4003 "not enough arguments for function-like macro invocation 'max'" plus syntax errors in Vector.h:114, Span.h:75, StaticVector.h:170. After restoring the fixed headers it builds clean
linux (Fedora Asahi aarch64, gcc) cmake -S . -B build -G Ninja && cmake --build build && ctest --test-dir build --output-on-failure -j8 (tmux) 563/563 passed (no Windows test, as expected)
studio (macOS, Apple clang) same, under nice (tmux) 563/563 passed
GitHub CI all 7 jobs All pass, including Windows MSVC/Clang on x64 and ARM64, so the regression TU also builds under clang-cl

The tests are pure unit tests with no windows or WebViews, so the win tests ran over ssh win.

Not verified

  • clang-format wasn't run: there's no toolchain on the machine where I edited. The changes only add parentheses, every line is within 85 columns, and the existing cast spacing is kept.

🤖 Generated with Claude Code

<windows.h> without NOMINMAX defines min and max as function-like macros,
which break calls like std::numeric_limits<int>::max() (Span.h's
Detail::fitsInt) in any consumer that includes it first. Wrapping the name in
parentheses suppresses macro expansion, so the headers no longer depend on the
consumer defining NOMINMAX.
Includes <windows.h> with NOMINMAX explicitly undefined, then every public
header, so an unparenthesized min/max call fails the Windows build.
@eyalamirmusic
eyalamirmusic marked this pull request as ready for review November 17, 2026 01:47
@eyalamirmusic
eyalamirmusic merged commit 0c281ff into main Nov 17, 2026
7 checks passed
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

Successfully merging this pull request may close these issues.

1 participant