Make mjsan.h compile under strict C11 with GCC - #3458
Open
teerthsharma wants to merge 1 commit into
Open
Conversation
mjsan.h replaces mj_markStack and mj_freeStack with sanitizer-aware definitions, so it is compiled only when a sanitizer is enabled. Two constructs in those definitions are rejected by GCC in strict C11 mode, which the project selects with -std=c11 -Wpedantic -Werror. The attribute follows the declarator in a function definition, which GCC rejects with "attributes should be specified before the declarator in a function definition". The barrier uses the bare keyword asm, which is not reserved in strict C11; GCC requires __asm__ there. Both are moved to the accepted spelling. The generated code is unchanged: always_inline still applies, and __asm__ volatile is the same barrier. Reproduction, on f9a00bd, Ubuntu 26.04, GCC 15.2.0: gcc -std=c11 -Wpedantic -Werror -fsanitize=address \ -Iinclude -Isrc -c src/engine/engine_collision_box.c -o /dev/null before exit 1, two errors at mjsan.h:58 and mjsan.h:65 after exit 0 Also verified after the change: clang 21.1.8 with -fsanitize=address exits 0, and GCC without a sanitizer exits 0, so the non-sanitizer path that skips this header is unaffected. Signed-off-by: teerthsharma <teerths57@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mjsan.hreplacesmj_markStackandmj_freeStackwith sanitizer-aware definitions, so it is compiled only when a sanitizer is enabled. Two constructs in those definitions are rejected by GCC in strict C11 mode, which the project selects with-std=c11 -Wpedantic -Werror.The attribute follows the declarator in a function definition, which GCC rejects with "attributes should be specified before the declarator in a function definition". The barrier uses the bare keyword
asm, which is not reserved in strict C11; GCC requires__asm__there.Both move to the accepted spelling. Generated code is unchanged:
always_inlinestill applies, and__asm__ volatileis the same barrier. Six lines, one file. There is no API, semantics, or non-sanitizer build change.Reproduction
On
f9a00bd5, Ubuntu 26.04, GCC 15.2.0:mjsan.h:58andmjsan.h:65, attributes should be specified before the declaratorAny translation unit reaching
mj_markStackormj_freeStackfails identically, so an ASan build of the library does not get past its first engine source file.Coverage
Measured after the change, same host and flags:
-fsanitize=address-fsanitize=addressThe third row is the control: without a sanitizer the header takes its other branch, so it confirms the ordinary build path is untouched.
Limits
Verified on Linux x86-64 with GCC 15.2.0 and clang 21.1.8 only. Older GCC accepts the trailing-attribute form, so this is a portability fix rather than a correctness fix, and it is invisible unless a sanitizer is enabled. Windows and macOS were not exercised, though neither construct is compiler-specific in the direction that would newly break them.