Skip to content

Remove nonnull attribute from sax_parse to prevent null check elision (#5048)#5089

Open
Nicolas0315 wants to merge 1 commit intonlohmann:developfrom
Nicolas0315:fix/remove-nonnull-from-sax-parse
Open

Remove nonnull attribute from sax_parse to prevent null check elision (#5048)#5089
Nicolas0315 wants to merge 1 commit intonlohmann:developfrom
Nicolas0315:fix/remove-nonnull-from-sax-parse

Conversation

@Nicolas0315
Copy link
Copy Markdown

Summary

Remove JSON_HEDLEY_NON_NULL from all three sax_parse() overloads to prevent the compiler from optimizing away the null safety check.

Problem

sax_parse() has a runtime null check:

if (sax == nullptr) {
    JSON_THROW(other_error::create(502, "SAX handler must not be null", nullptr));
}

But the parameter is also annotated with JSON_HEDLEY_NON_NULL, which expands to __attribute__((__nonnull__)). This tells the compiler that null is never passed, allowing it to eliminate the null check entirely in optimized builds (-O2 -DNDEBUG).

Result: instead of a proper exception, users get a segfault.

The existing #pragma diagnostic workaround only suppresses the warning — it does not prevent the optimization.

Fix

  • Remove JSON_HEDLEY_NON_NULL from all three sax_parse overloads
  • Remove the now-unnecessary #pragma clang/GCC diagnostic blocks around the null checks
  • Applied to both include/nlohmann/json.hpp and single_include/nlohmann/json.hpp

The null check is the correct safety mechanism. The nonnull attribute contradicts it.

Testing

Compiles cleanly with clang++ -std=c++11 -O2 -DNDEBUG.

Fixes #5048

Clang with -O2 and -DNDEBUG optimizes away the 'sax == nullptr'
safety check in sax_parse() because the parameter is annotated with
JSON_HEDLEY_NON_NULL. This makes the compiler assume nullptr is
never passed, allowing it to eliminate the check entirely. The
result is a segfault instead of a proper exception.

The existing #pragma workaround only suppresses the compiler
warning but does not prevent the optimization.

Remove JSON_HEDLEY_NON_NULL from all three sax_parse overloads
(and the corresponding diagnostic pragmas that are no longer needed)
in both include/ and single_include/ headers.

Fixes nlohmann#5048
@Nicolas0315 Nicolas0315 requested a review from nlohmann as a code owner March 1, 2026 19:15
@github-actions github-actions bot added the M label Mar 1, 2026
@gregmarr
Copy link
Copy Markdown
Contributor

gregmarr commented Mar 1, 2026

As described in #5048: Anyway, the non null is the important part. The exception is just a minor safety net. If anything should be removed, it's that, imho.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

@github-actions github-actions bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

M state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Projects

None yet

Development

Successfully merging this pull request may close these issues.

function argument safety check silently optimized out in release build by clang

2 participants