Skip to content

BUG: Use memcpy instead of casting to over-aligned pointer types - #116

Merged
hjmjohnson merged 1 commit into
masterfrom
pr/fix-cast-align-memcpy
Nov 14, 2026
Merged

hjmjohnson merged 1 commit into
masterfrom
pr/fix-cast-align-memcpy

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #44, reverted from master on 2026-09-24 so it can be
reviewed before merging. Content is unchanged from the original.

Base: master. Independent: nothing has to land before it.

Commits
  • BUG: Use memcpy instead of casting to over-aligned pointer types

Ordering for all the re-submitted work is tracked in #84.

@hjmjohnson
hjmjohnson marked this pull request as ready for review September 24, 2026 12:17
19 -Wcast-align warnings, and behind them undefined behavior on any
target that cares about alignment.

modify_field() writes a value into a header field at a byte offset
parsed from a field table:

    ((short *)((char *)basep + field->offset))[fc] = (short)val;

field->offset is a byte offset into a packed on-disk header, so that
cast produces an address only correctly aligned by coincidence.  The
same pattern appears for int, int64_t, float and double, in both tool
files.  Each becomes a memcpy of the right width at the right byte
offset.

The second group reads a pointer back out of a structure through a
byte offset -- `sp = *(char **)((char *)str + fp->offset)` and the
nifti1_extension equivalents -- and becomes a memcpy into an aligned
local.

The third is nifti_header_version(), which cast its `const char * buf`
argument, a buffer straight off a file read with no alignment
guarantee, to both nifti_1_header * and nifti_2_header * and read
fields through them.  It now copies into aligned locals first, exactly
the sizeof(nifti_1_header) bytes the function already checks are
present.

Verified by round-tripping int16, int32, int64, float32, float64 and
string fields through nifti_tool -mod_hdr2.

(cherry picked from commit eb4f831)
@hjmjohnson
hjmjohnson force-pushed the pr/fix-cast-align-memcpy branch from 0f11ac1 to e803f39 Compare October 6, 2026 14:34

@gdevenyi gdevenyi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Google Carbon

Verdict: Approve

Verified all 19 -Wcast-align sites across nifti2/nifti2_io.c, nifti2/nifti_tool.c, and niftilib/nifti1_tool.c:

  • Confirming -Wcast-align (Clang) and -Wcast-align=strict (GCC) drop from 19 warnings to 0 across the entire repository!
  • Beyond alignment UB, nifti_header_version (nifti2/nifti2_io.c:6586-6588) also fixes a real C11 object-bounds violation: nifti_read_header only allocates/reads sizeof(nifti_1_header) (348 bytes) before calling nifti_header_version, so forming *n2p (nifti_2_header, 540 bytes) over a 348-byte buffer was undefined behavior even when aligned. Copying sizeof(n1hdr) bytes into a zeroed stack n2hdr is completely sound because sizeof_hdr (offset 0..3) and magic (offset 4..11) both lie within the first 12 bytes of nifti_2_header.

Left two small non-blocking nits inline.

Comment thread nifti2/nifti2_io.c Resolved
Comment thread nifti2/nifti_tool.c
ext0 = *(nifti1_extension **)((char *)str0 + fp->offset);
ext1 = *(nifti1_extension **)((char *)str1 + fp->offset);
memcpy(&ext0, (const char *)str0 + fp->offset, sizeof(ext0));
memcpy(&ext1, (const char *)str1 + fp->offset, sizeof(ext1));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Google Carbon] Minor C11 pedantry nit (here and line 6333's NT_DT_POINTER case above, plus niftilib/nifti1_tool.c:3552, 3558-3559): line 6333 still compares two pointer fields via memcmp(p0, p1, sizeof(void *)) rather than memcpying into void *vp0, *vp1; and testing if (vp0 != vp1) (strictly speaking, segmented/tagged-pointer representations can equalize pointers with differing bit patterns, though identical on flat ABIs).

@hjmjohnson
hjmjohnson merged commit 825af15 into master Nov 14, 2026
4 checks passed
@hjmjohnson
hjmjohnson deleted the pr/fix-cast-align-memcpy branch November 14, 2026 14:47
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.

3 participants