Skip to content

ENH: Pass calloc its arguments in the documented order - #50

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:masterfrom
gdevenyi:pr/fix-calloc-transposed-args
Sep 23, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:masterfrom
gdevenyi:pr/fix-calloc-transposed-args

Conversation

@gdevenyi

@gdevenyi gdevenyi commented Aug 15, 2026 •

Copy link
Copy Markdown

Four calls were written calloc(sizeof(char), n) rather than
calloc(n, sizeof(char)), which gcc 14 and later diagnose as
-Wcalloc-transposed-args.

There is no behaviour change: calloc multiplies its two arguments and
sizeof(char) is 1 by definition. The warning is worth clearing anyway,
because the diagnostic exists to catch the case where the element size is
not 1, at which point the transposition produces a buffer of the wrong
size. Leaving benign instances in the tree trains the reader to ignore
the warning that will one day be real.

Eight further instances are left alone deliberately: they are inside
nifti_findhdrname(), nifti_findimgname(), nifti_makehdrname() and
nifti_makeimgname(), which upstream PR #24 rewrites. That PR keeps the
transposed order, so they will need fixing on top of it; changing them
here would only create a conflict.


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
@hjmjohnson

Copy link
Copy Markdown
Member

Rebased onto master (b4876bf) and cleaned the commit message. Code diff unchanged (4 lines).

No regression test applies, and nothing observable exists to test. calloc(n, size) and calloc(size, n) return the same number of zeroed bytes, and the implementation's overflow check is on the same product either way. With size being sizeof(char) — 1 by definition — the two forms are indistinguishable at runtime. Class is BUILD: the effect is that gcc 14+ stops emitting -Wcalloc-transposed-args.

Message cleanup

Removed the Co-Authored-By: trailer naming an AI tool and the Claude-Session: URL; "behaviour" to "behavior". Author and author date preserved (Gabriel A. Devenyi, 2026-08-14).

Replaced the bare #24 cross-reference with a description in words — this is a fork, so a bare number resolves to a different PR upstream. The number belongs in this PR's description, not in permanent history.

Also corrected a count: the body said "Eight further instances are left alone". There are ten on current master:

nifti2/nifti2_io.c:3752,3768,3841,3928,4003
niftilib/nifti1_io.c:2814,2830,2903,2989,3059

five per io file, in nifti_findhdrname(), nifti_findimgname(), nifti_makehdrname() and nifti_makeimgname().

Build and test
  • Release, NIFTI_BUILD_APPLICATIONS=ON USE_NIFTI2_CODE=ON USE_CIFTI_CODE=ON USE_FSL_CODE=ON: 100% tests passed, 0 tests failed out of 362.
  • Same plus -DFSLSTYLE=ON (this PR touches fslio.c): 100% tests passed, 0 tests failed out of 364.

Whitespace churn check: git diff master HEAD and git diff -w master HEAD both report 4 added lines.

@hjmjohnson
hjmjohnson force-pushed the pr/fix-calloc-transposed-args branch from 530211e to edcc0ea Compare September 22, 2026 20:37
Four calls were written calloc(sizeof(char), n) rather than
calloc(n, sizeof(char)), which gcc 14 and later diagnose as
-Wcalloc-transposed-args.

There is no behavior change: calloc multiplies its two arguments and
sizeof(char) is 1 by definition, so the same number of zeroed bytes is
returned either way and the overflow check is the same product.  The
warning is worth clearing anyway, because the diagnostic exists to
catch the case where the element size is not 1, at which point the
transposition produces a buffer of the wrong size.  Leaving benign
instances in the tree trains the reader to ignore the warning that
will one day be real.

Ten further instances are left alone deliberately.  They are in
nifti_findhdrname(), nifti_findimgname(), nifti_makehdrname() and
nifti_makeimgname() in both io files, all of which an open pull
request rewrites while keeping the transposed order.  They will need
fixing on top of that work; changing them here would only conflict
with it.
@hjmjohnson
hjmjohnson force-pushed the pr/fix-calloc-transposed-args branch from edcc0ea to 0a33e97 Compare September 23, 2026 00:47
@hjmjohnson
hjmjohnson merged commit affba1e into InsightSoftwareConsortium:master Sep 23, 2026
26 checks passed
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