Skip to content

BUG: Keep the XML skip depth at the element that started the skip - #125

Draft
hjmjohnson wants to merge 3 commits into
stack/pr-fix-alloc-null-checksfrom
stack/fix-axml-skip-depth
Draft

hjmjohnson wants to merge 3 commits into
stack/pr-fix-alloc-null-checksfrom
stack/fix-axml-skip-depth

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #65, reverted from master on 2026-09-24. Content is
unchanged from the original.

Position 3 of 11 in the deep stack. Base: stack/pr-fix-alloc-null-checks.

Based on the pull request above it in the stack, so the diff shown here is
this change alone. Merge the stack bottom-up.

Stack order

# branch base
1 stack/test-regression-coverage master
2 stack/pr-fix-alloc-null-checks stack/test-regression-coverage
3 stack/fix-axml-skip-depth <- this PR stack/pr-fix-alloc-null-checks
4 stack/pr-fix-analyzer-leaks stack/fix-axml-skip-depth
5 stack/pr-fix-sign-conversion stack/pr-fix-analyzer-leaks
6 stack/fix-fslio-64bit-arithmetic stack/pr-fix-sign-conversion
7 stack/fix-cifti-null-stream stack/fix-fslio-64bit-arithmetic
8 stack/pr-fix-calloc-transposed-args stack/fix-cifti-null-stream
9 stack/pr-fix-shorten-64-to-32 stack/pr-fix-calloc-transposed-args
10 stack/fix-image-read-complex-check stack/pr-fix-shorten-64-to-32
11 stack/pr-fix-xml-read-errors stack/fix-image-read-complex-check

The order is the order these changes sat on master before the revert, so
it builds and tests at every step.

Commits introduced by this PR
  • BUG: Keep the XML skip depth at the element that started the skip
  • BUG: Do not assume a popped element filled its stack slot
  • ENH: Cover XML nesting past the afni_xml stack limit

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
@hjmjohnson
hjmjohnson marked this pull request as draft October 6, 2026 15:02
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-alloc-null-checks branch from 48f6b68 to 9136ec2 Compare November 2, 2026 15:32
@hjmjohnson
hjmjohnson force-pushed the stack/fix-axml-skip-depth branch 2 times, most recently from cd03008 to fe446fe Compare November 6, 2026 14:59
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-alloc-null-checks branch from 9136ec2 to 924169e Compare November 6, 2026 14:59

@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: Request Changes (Draft; unbundle hitchhiking #111 + minor OOM leak note; XML dskip state machine verified exact!)

Traced every depth transition in epush and epop (cifti/afni_xml.c) across depth = 1..16, depth = 17 (AXML_MAX_DEPTH + 1), and nested depth = 18+ elements:

  • Moving if (xd->dskip == xd->depth) xd->dskip = 0; before epop_xml and keeping the outermost skip depth via if (errs && !xd->dskip) xd->dskip = xd->depth; is mathematically exact and completely eliminates the out-of-bounds xd->stack[16] read/write when an XML document exceeds AXML_MAX_DEPTH (16)!
  • The defensive bounds and NULL guards inside epop_xml (lines 805-815) are also solid.

Changes requested before merging:

  1. Unbundle PR #111 (znzread / nifti_read_buffer short-read fix): This branch silently bundles #111 alongside the XML depth-skip fix. Please rebase onto master so #125 contains only the cifti/afni_xml.c + test commit.
  2. Pre-existing OOM leak interaction in epush (cifti/afni_xml.c:739-750): Note inline below on freeing acur if add_to_xroot_list / add_to_xchild_list fails when setting xd->dskip.

Comment thread cifti/afni_xml.c Resolved
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-alloc-null-checks branch from 924169e to b56f419 Compare November 17, 2026 22:50
@hjmjohnson
hjmjohnson force-pushed the stack/fix-axml-skip-depth branch from fe446fe to 977b40c Compare November 17, 2026 22:50
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the updated #124. This PR's own fix: epush frees an element it could not attach (67cfbb4). 6491337 is already on master as 8835cc7 and drops in the rebase onto master.

gdevenyi and others added 3 commits November 19, 2026 13:00
afni_xml's parser holds sixteen levels of open elements in a fixed
array and refuses to push a seventeenth by entering a skip block:
epush() records the depth in xd->dskip and stops touching the stack
until the matching pop.  Two details of that bookkeeping are wrong.

epush() overwrote dskip on every push past the limit, so the skip was
recorded as starting at the innermost depth.  epop() then cleared
dskip before the stack was handled, so the pop that ends the skip fell
through to "xd->stack[xd->depth-1] = NULL" and wrote a pointer eight
bytes past the sixteen-entry array, which lives in the file-scope
afni_xml_control.  The next pop found dskip clear and dereferenced
xd->stack[16].

epush() now keeps the outermost skip depth, since that is the depth
whose pop ends the skip, and epop() clears dskip only after the
element has been skipped.  Depths within the limit are unaffected.

The XML comes from the CIFTI extension of a NIfTI file, or from any
file read by axml_read_file(), so the nesting is chosen by the input.
Found by fuzzing axml_read_buf() with libFuzzer under
AddressSanitizer.

(cherry picked from commit 6ddc454)
process_popped_element() read xd->stack[xd->depth-1] and dereferenced
it without a test.  A slot is filled by the matching epush(), and an
element that was skipped never fills one, so the read is only safe
because the preceding commit keeps the skip running to the element
that started it.

Guard it rather than rely on that: check the depth is inside the
stack, and that the slot holds a struct with a name, before the
strcmp().

(cherry picked from commit d34f28c)
deep_nesting.xml nests twenty elements inside a CIFTI extension whose
stack holds sixteen, which is the shape that walked off the end of
xd->stack.  The test drives it through cifti_tool and pins that the
BrainModel sibling is still summarized, so a fix that refused the
whole document would not pass.

(cherry picked from commit 0e0033d)
@hjmjohnson
hjmjohnson force-pushed the stack/pr-fix-alloc-null-checks branch from b56f419 to 743ace9 Compare November 19, 2026 19:01
@hjmjohnson
hjmjohnson force-pushed the stack/fix-axml-skip-depth branch from 977b40c to 22bb490 Compare November 19, 2026 19:01
@hjmjohnson

Copy link
Copy Markdown
Member Author

Restacked on the rebased #124 (22bb490). Master already carries newer copies of two of this PR's commits (the size_t return fix and the truncated-image test), so those are dropped.

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