Skip to content

BUG: Bound the aux_file copy by sizeof rather than a repeated 24 - #46

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:masterfrom
gdevenyi:pr/fix-stringop-truncation
Sep 21, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:masterfrom
gdevenyi:pr/fix-stringop-truncation

Conversation

@gdevenyi

@gdevenyi gdevenyi commented Aug 15, 2026 •

Copy link
Copy Markdown

FslSetAuxFile() copied with strncpy(dest, src, 24) into a char[24] and
then wrote the terminator at [24-1]. That is correct, but only because
of the second line: strncpy() writes no terminator when the source is 24
characters or longer, which is what -Wstringop-truncation points at.

Copy at most sizeof(dest) - 1 and terminate at sizeof(dest) - 1, so the
call is self-evidently safe without depending on the line after it, and
the buffer size comes from the buffer rather than a literal repeated in
two places. Follows the direction of commit 7b08ead, "Use sizeof()
instead of repeating raw constant".

Behaviour is unchanged for every input.


Interface impact: none. On the union of all these changes, configured with USE_FSL_CODE=ON and USE_CIFTI_CODE=ON: all 448 exported symbols across libniftiio, libnifti2, libznz, libfslio, libnifticdf and libcifti are identical to master under nm -D --defined-only, and all ten installed headers are identical under gcc -E -P. Under gcc -dM -E one macro definition differs, intentionally and only in text: #61 makes FSL_RADIOLOGICAL read (-1) so it is safe inside an expression. Its value is still -1, checked by compiling against each installed fslio.h and printing it.

Verification. This branch: builds with gcc 16.1.1, ctest unchanged from master (2 of 345 fail on master itself in this environment; #31 and #29 each fix one). The union of all the PRs: 0 errors under both gcc 16.1.1 and clang 22.1.8, ctest 345/345 under each, and the whole suite under valgrind memcheck with --trace-children=yes gives 484 traced processes with no invalid access, no uninitialised value and no leak in any nifti binary.

Coordination. Every line of every branch was compared, whitespace-normalised, against the diffs of the open PRs (#11, #21, #22, #23, #24). Where one of those already changes a line, the line was left alone, and the few deliberate overlaps are named in the text above. What survives is 17 compiler warnings, all of them on those lines: 9 -Wsign-conversion (5 in fslio.c for #22, 2 in nifti2_io.c and 2 in nifti_tester001.c for #24) and 8 -Wcalloc-transposed-args in nifti_findhdrname and nifti_findimgname, which #11 rewrites. No formatting changes appear anywhere, to stay clear of #10 and #12.

One of a set of independent, single-purpose PRs. Each bases on master and can be merged on its own, in any order.

The full set of PRs (35)

The union of all of them is on the fork as all-changes, if you want to build and test the lot at once.

CI and build

Configuration and documentation

Defects

Warning and check classes

This was referenced Aug 15, 2026
strncpy writes no terminator when the source fills the destination, so
the copy is safe only because of the line that follows it, which is
what -Wstringop-truncation reports.

Copying at most sizeof(dest) - 1 makes the call safe on its own and
takes the size from the buffer rather than a literal repeated twice.
@hjmjohnson
hjmjohnson force-pushed the pr/fix-stringop-truncation branch from 4452889 to 6f45656 Compare September 21, 2026 23:12
@hjmjohnson

Copy link
Copy Markdown
Member

Rebased onto current master now that #37 has merged. One commit, unchanged in substance.

With #37 in, this is the last GCC warning in the default configuration: FSLSTYLE=OFF goes from 1 to 0 on GCC 13.3.

Commit message trimmed

The body narrated the previous form of the code and used a British spelling. It now states only what the change is for: strncpy writes no terminator when the source fills the destination, so the copy was safe only by virtue of the line after it, and copying at most sizeof(dest) - 1 makes the call safe on its own. Authorship is unchanged.

The getter half landed in #37

FslGetAuxFile had the same literal 24 in its bound and terminator index. #37 changed it to derive both from sizeof, so the pair stays symmetric once this merges and neither can drift if the field width ever changes.

Verification

Checked on macOS with AppleClang after the rebase: builds clean, 345/345 tests, no warnings. AppleClang does not implement -Wstringop-truncation, so that part rests on the GCC 13.3 run made before the rebase, which showed the warning cleared. The rebase moved this commit only across #37's changes, which have since merged.

@hjmjohnson
hjmjohnson merged commit 33ac84e into InsightSoftwareConsortium:master Sep 21, 2026
17 of 21 checks passed
@hjmjohnson

Copy link
Copy Markdown
Member

The commit messages in this range were rewritten to remove trailers that do not belong in permanent history: Co-Authored-By: naming an AI tool, and Claude-Session: URLs that resolve for nobody. Only messages changed — the tree at the tip of master is byte-identical, and author, committer, and dates are preserved.

This PR's commit on the rewritten master:

  • 5c1665c6d9 BUG: Bound the aux_file copy by sizeof rather than a repeated 24

The SHA recorded above by GitHub is from the pre-rewrite history and no longer resolves.

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