Skip to content

Update a couple of functions to know buffer length - #121

Merged
hjmjohnson merged 1 commit into
masterfrom
missing-buffer-len
Nov 14, 2026
Merged

hjmjohnson merged 1 commit into
masterfrom
missing-buffer-len

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #21, 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
  • ENH: Bound the field-modification helpers by the destination size

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:18
modify_all_fields() and modify_field() wrote into a caller-supplied
buffer at an offset taken from the field table, with no way to check that
the write stayed inside it. Both now take the buffer size, and
modify_field() rejects a field whose offset plus size * len exceeds it.

The check sits ahead of the switch, so it covers every write path rather
than the string case alone, and it reports and returns like the other
failures in the function; an assert() would compile away in the release
builds that ship.

No field table can trip it today: check_total_size() already requires the
offsets to tile the structure exactly. It bounds future edits to them.

(cherry picked from commit 7356eb1)

@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 (with non-blocking hardening nits)

Verified ABI safety and call sites across nifti2/ and niftilib/:

  • Neither nifti2/nifti_tool.h nor niftilib/nifti1_tool.h is installed, and nifti_tool.c / nifti1_tool.c are compiled only into the CLI executables (nifti_tool, nifti1_tool), not into libnifti2 or libniftiio (NI2_API on nifti_tool.h is a historical artifact without -DNIFTI2_BUILD_SHARED on the executable target). Zero shared-library ABI impact.
  • Verified every static field_s table entry (g_hdr1_fields, g_hdr2_fields, g_nim2_fields, g_hdr_fields, g_nim_fields) fits well inside sizeof(*nhdr) / sizeof(*nim).

Left two non-blocking defensive nits inline (field->size <= 0 + overflow-free size_t arithmetic on 32-bit, and parameter-name consistency between .h and .c).

Comment thread nifti2/nifti_tool.c

/* every case below writes field->len elements at field->offset */
if( field->offset < 0 || field->size < 0 || field->len < 0 ||
(size_t)field->offset + (size_t)field->size * (size_t)field->len > baselen )

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] Non-blocking defensive nit (here and in niftilib/nifti1_tool.c:2917-2918):

  1. Tighten field->size < 0 to field->size <= 0 (every valid NT_DT_* field has positive element size).
  2. On 32-bit platforms (sizeof(size_t) == 4), (size_t)field->offset + (size_t)field->size * (size_t)field->len can wrap modulo 2^32 if a caller passes a malformed field_s with huge len/offset. Writing:
if( field->offset < 0 || field->size <= 0 || field->len < 0 ||
    (size_t)field->offset > baselen ||
    (size_t)field->len > (baselen - (size_t)field->offset) / (size_t)field->size )

is 100% overflow-free on both 32-bit and 64-bit.

Comment thread nifti2/nifti_tool.h
NI2_API int fill_ana_field_array(field_s * ah_fields);
NI2_API int modify_all_fields(void *basep, nt_opts *opts, field_s *fields, int flen);
NI2_API int modify_field (void * basep, field_s * field, const char * data);
NI2_API int modify_all_fields(void *basep, size_t baseplen, nt_opts *opts, field_s *fields, int flen);

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 naming nit: the headers (nifti2/nifti_tool.h:309-310 and niftilib/nifti1_tool.h:143-144) name the parameter baseplen, whereas the definitions in .c name it baselen (readability-named-parameter / consistency). Also note niftilib/nifti1_tool.c:2860 lacks nifti2's !basep NULL check.

@hjmjohnson
hjmjohnson merged commit 7504b3b into master Nov 14, 2026
4 checks passed
@hjmjohnson
hjmjohnson deleted the missing-buffer-len branch November 14, 2026 14:45
@seanm

seanm commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Oh I was going to fix the naming inconsistency the bot pointed out... I guess I'll make a followup PR...

@seanm

seanm commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

#140 fixes the naming inconsistency and restores things to what my original PR was, and notably fixes the misattribution of some of the LLM changes to me.

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