Skip to content

Systematic memory safety hardening#171

Merged
Taywee merged 5 commits into
Taywee:masterfrom
metsw24-max:systematic-hardening-checked-arithmetic
May 22, 2026
Merged

Systematic memory safety hardening#171
Taywee merged 5 commits into
Taywee:masterfrom
metsw24-max:systematic-hardening-checked-arithmetic

Conversation

@metsw24-max
Copy link
Copy Markdown
Contributor

@metsw24-max metsw24-max commented May 21, 2026

What This PR Changes

Compiler Hardening Support

Adds optional compiler hardening and sanitizer support:

GCC / Clang

  • -Wall
  • -Wextra
  • -Wconversion
  • -Wsign-conversion
  • -Wformat=2
  • Optional ASan / UBSan support

MSVC

  • /W4
  • /sdl
  • /GS
  • /guard:cf

Sanitizers remain disabled by default and can be enabled explicitly:

cmake -DENABLE_ASAN=ON -DENABLE_UBSAN=ON

Fuzzing Infrastructure

Adds an optional libFuzzer-based parser fuzz target:

  • fuzz/fuzz_parser.cpp
  • OSS-Fuzz compatible setup
  • helps detect parsing crashes and undefined behavior

Fuzzing is disabled by default and can be enabled with:

cmake -DBUILD_FUZZERS=ON

Why This Change

This PR improves robustness around arithmetic operations, parser edge cases, and undefined behavior handling while adding infrastructure for future security testing and regression detection.

The changes are fully backward compatible and introduce no public API modifications.

Files Added / Modified

  • fuzz/fuzz_parser.cpp
  • fuzz/README.md
  • CMakeLists.txt

Compatibility

  • No API changes
  • No behavior changes for existing consumers
  • All existing tests pass successfully
  • Hardening and fuzzing features are opt-in

These duplicated functionality already provided by args.hxx
(SafeAdd/SafeMultiply/SafeSub/SafeNeg in namespace args) and
already covered by test/safe_arithmetic.cxx.

The duplicate header used an unsigned-only `value < min()` check
that triggers -Wtype-limits under -Werror on gcc/clang, breaking
the ubuntu and macos CMake jobs. Meson and Windows were unaffected
because meson.build did not list the duplicate test and MSVC does
not emit that warning.
@metsw24-max metsw24-max force-pushed the systematic-hardening-checked-arithmetic branch from 9019222 to 00d2a8b Compare May 22, 2026 04:33
vector::erase(begin() + idx) implicitly converts size_t -> ptrdiff_t.
Under the branch's new directory-wide -Wsign-conversion + -Werror,
this aborts the build of every test TU that instantiates Parse().
Add an explicit static_cast to the vector's difference_type.
This warning fires on every raw pointer arithmetic operation, including
the standard `argv + 1, argv + argc` idiom used in args.hxx and the
gitlike example. Combined with the test target's -Werror, it broke all
test TUs on macos/clang. There is no clean replacement for argv handling
short of std::span (C++20), so leave the flag off for this C++11 codebase.
@Taywee
Copy link
Copy Markdown
Owner

Taywee commented May 22, 2026

I'm happy with this. Thank you for the PR.

@Taywee Taywee merged commit 93a3e5f into Taywee:master May 22, 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.

2 participants