Skip to content

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

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

Suppress spurious -Wcast-align in the arena allocator#43
brtnfld wants to merge 1 commit into
cktan:mainfrom
brtnfld:fix/cast-align-cell-realloc

Conversation

@brtnfld

@brtnfld brtnfld commented Jul 16, 2026

Copy link
Copy Markdown

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

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.
@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
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]%

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