Skip to content

Suppress spurious -Wcast-align in the arena allocator - #43

Open
brtnfld wants to merge 2 commits into
cktan:mainfrom
brtnfld:fix/cast-align-cell-realloc
Open

brtnfld wants to merge 2 commits into
cktan:mainfrom
brtnfld:fix/cast-align-cell-realloc

Conversation

@brtnfld

@brtnfld brtnfld commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

cell_realloc()'s header-before-payload pattern and its call sites that cast the returned char* back to a wider-aligned pointer type trigger -Wcast-align under -Werror. The casts are safe: p is always either REALLOC()'s own return value, or p - sizeof(cell_t) recovers that exact same, maximally-aligned pointer -- the compiler just can't prove it from byte-level pointer arithmetic on a char*. Route each cast through an intermediate (void *), the standard idiom for a deliberate alignment-increasing cast. No behavioral change.

@cktan

cktan commented Jul 18, 2026

Copy link
Copy Markdown
Owner

can't repro

@brtnfld
brtnfld force-pushed the fix/cast-align-cell-realloc branch from 16c7299 to 51e6673 Compare July 18, 2026 20:43
@brtnfld

brtnfld commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Re "can't repro": the warning only shows up if -Wcast-align is explicitly enabled — it's not part of -Wall/-Wextra, and the project's default src/Makefile CFLAGS (-std=c17 -Wmissing-declarations -Wall -Wextra -MMD) don't turn it on, so a plain make won't trigger it.

Steps to reproduce (against main before this PR):

git checkout main -- src/tomlc17.c
cc -std=c17 -Wall -Wextra -Wcast-align -Werror -c src/tomlc17.c -o /tmp/t.o

That fails with 8 -Wcast-align errors, e.g.:

src/tomlc17.c:224:16: error: cast from 'char *' to 'cell_t *' (aka 'struct cell_t *') increases required alignment from 1 to 4 [-Werror,-Wcast-align]
  cell_t *cp = (cell_t *)(p - sizeof(cell_t));

Building the same command against this branch's src/tomlc17.c compiles clean.

I also rebased the branch onto the latest main (your recent "cleanup" commit had reformatted a couple of the same lines this PR touches, which was causing the merge conflict) and re-verified with the -Wcast-align -Werror build plus make test (all suites pass except stdtest, which needs go/toml-test not installed in my sandbox — unrelated to this change). PR should be mergeable now.

@cktan

cktan commented Jul 20, 2026

Copy link
Copy Markdown
Owner

i don't see it on ubuntu...

[cktan]%
[cktan]% cc -std=c17 -Wall -Wextra -Wcast-align -Werror -c src/tomlc17.c -o /tmp/t.o
[cktan]%

cell_realloc()'s header-before-payload pattern and its call sites
that cast the returned char* back to a wider-aligned pointer type
trigger -Wcast-align under -Werror. The casts are safe: p is always
either REALLOC()'s own return value, or p - sizeof(cell_t) recovers
that exact same, maximally-aligned pointer -- the compiler just can't
prove it from byte-level pointer arithmetic on a char*. Route each
cast through an intermediate (void *), the standard idiom for a
deliberate alignment-increasing cast. No behavioral change.
The review of this PR stalled on "can't repro": the same command that
reports eight errors for me reports none on an x86-64 Ubuntu box. Both
observations are correct, and the reason is the compiler.

-Wcast-align is not implied by -Wall or -Wextra, and gcc's plain
-Wcast-align is a no-op on x86-64 -- gcc only reports it on targets that
genuinely require the stricter alignment. clang reports it on any target.
So `cc -Wcast-align` is silent when cc is gcc on x86-64, and noisy when cc
is clang, which is exactly the split in the thread. Measured on the commit
before this fix:

  gcc   -Wall -Wextra -Wcast-align           ->  0 diagnostics
  gcc   -Wall -Wextra -Wcast-align=strict    ->  8 diagnostics
  clang -Wall -Wextra -Wcast-align           ->  8 diagnostics

and on this fix, all three report 0.

Rather than leave that as a footnote in a comment thread, add a CI job
running both spellings with -Werror, so the warning is reproducible on
either compiler and a regression shows up as a failing check instead of
an argument. Verified that the job's exact commands pass on this commit
and fail on its parent, so it is not vacuous.

Also rebased onto current main: the branch was ten commits behind, and the
merge is clean with no new cast sites introduced in the meantime.
@brtnfld
brtnfld force-pushed the fix/cast-align-cell-realloc branch from 51e6673 to 029dc93 Compare September 8, 2026 14:07
@brtnfld

brtnfld commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Found it -- we're both right, and it's the compiler.

-Wcast-align is not implied by -Wall/-Wextra, and gcc's plain
-Wcast-align is a no-op on x86-64
: gcc only reports it on targets that
genuinely require the stricter alignment. clang reports it on any target.
So cc -Wcast-align is silent when your cc is gcc on x86-64, and noisy
when mine is clang. That's the whole disagreement. Sorry -- I should have
said which compiler I was using instead of just pasting the command.

Measured on the commit before the fix:

gcc   -Wall -Wextra -Wcast-align           ->  0 diagnostics
gcc   -Wall -Wextra -Wcast-align=strict    ->  8 diagnostics
clang -Wall -Wextra -Wcast-align           ->  8 diagnostics

and on this branch, all three report 0.

To reproduce on your Ubuntu box without installing anything, add =strict:

gcc -std=c17 -Wall -Wextra -Wcast-align=strict -Werror -c src/tomlc17.c -o /dev/null

Rather than leave that buried in this thread, I've pushed a CI job
(029dc93) that runs both spellings with -Werror, so it is reproducible
on either compiler and any regression shows up as a failing check. I
checked the job's exact commands pass on the fix commit and fail on its
parent, so it isn't vacuous.

Also rebased onto current main -- the branch had fallen ten commits
behind. The merge is clean and none of the intervening commits introduced
new cast sites, so the fix is still complete: the merged tree reports 0
under both gcc -Wcast-align=strict and clang -Wcast-align.

Entirely reasonable to take the CI job and skip the source change if you
would rather not carry the (void *) casts -- though then the job would
need to come out too, since it would fail on main as it stands.

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