Repository navigation
Update a couple of functions to know buffer length - #121
Conversation
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)
d587800 to
d9b6a12
Compare
gdevenyi
left a comment
There was a problem hiding this comment.
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.hnorniftilib/nifti1_tool.his installed, andnifti_tool.c/nifti1_tool.care compiled only into the CLI executables (nifti_tool,nifti1_tool), not intolibnifti2orlibniftiio(NI2_APIonnifti_tool.his a historical artifact without-DNIFTI2_BUILD_SHAREDon the executable target). Zero shared-library ABI impact. - Verified every static
field_stable entry (g_hdr1_fields,g_hdr2_fields,g_nim2_fields,g_hdr_fields,g_nim_fields) fits well insidesizeof(*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).
|
|
||
| /* 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 ) |
There was a problem hiding this comment.
[Google Carbon] Non-blocking defensive nit (here and in niftilib/nifti1_tool.c:2917-2918):
- Tighten
field->size < 0tofield->size <= 0(every validNT_DT_*field has positive element size). - On 32-bit platforms (
sizeof(size_t) == 4),(size_t)field->offset + (size_t)field->size * (size_t)field->lencan wrap modulo2^32if a caller passes a malformedfield_swith hugelen/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.
| 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); |
There was a problem hiding this comment.
[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.
|
Oh I was going to fix the naming inconsistency the bot pointed out... I guess I'll make a followup PR... |
|
#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. |
Re-submission of #21, 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.