Repository navigation
BUG: Use memcpy instead of casting to over-aligned pointer types - #116
Merged
Merged
Conversation
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
force-pushed
the
pr/fix-cast-align-memcpy
branch
from
October 6, 2026 14:34
0f11ac1 to
e803f39
Compare
gdevenyi
approved these changes
Nov 12, 2026
gdevenyi
left a comment
Collaborator
There was a problem hiding this comment.
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_headeronly allocates/readssizeof(nifti_1_header)(348 bytes) before callingnifti_header_version, so forming*n2p(nifti_2_header, 540 bytes) over a 348-byte buffer was undefined behavior even when aligned. Copyingsizeof(n1hdr)bytes into a zeroed stackn2hdris completely sound becausesizeof_hdr(offset 0..3) andmagic(offset 4..11) both lie within the first 12 bytes ofnifti_2_header.
Left two small non-blocking nits inline.
| 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)); |
Collaborator
There was a problem hiding this comment.
[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).
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.
Re-submission of #44, reverted from
masteron 2026-09-24 so it can bereviewed before merging. Content is unchanged from the original.
Base:
master. Independent: nothing has to land before it.Commits
Ordering for all the re-submitted work is tracked in #84.