Skip to content

Fix null/out-of-range access in the switch and swizzle version upgrade - #3069

Merged
jstone-lucasfilm merged 6 commits into
AcademySoftwareFoundation:mainfrom
kshivam4781:fix/issue3067-3068-version-upgrade-null-checks
Sep 24, 2026
Merged

jstone-lucasfilm merged 6 commits into
AcademySoftwareFoundation:mainfrom
kshivam4781:fix/issue3067-3068-version-upgrade-null-checks

Conversation

@kshivam4781

Copy link
Copy Markdown
Contributor

Fixes #3067 and #3068.

Both are fuzz findings from @seang-ilm on the same code path: Document::upgradeVersion()'s 1.38-to-1.39 step for switch and swizzle nodes.

#3067 (nulldref on switch): which->getValue() returns null when the stored value string can't be parsed as the input's declared type (e.g. value="abc" on an integer input). The existing code called whichValue->isA<int>() without checking for that, so a bad which value crashes the upgrade instead of just being left alone. Fix adds a null check before both isA/asA calls.

#3068 (out-of-range on swizzle): when a swizzle node's in input has an empty value (value=""), origValueTokens ends up empty, but the fallback branch for an invalid/missing channel name unconditionally did origValueTokens[0]. Fix falls back to "0" when origValueTokens is empty, matching the existing convention in this same function of treating a missing/unparseable value as 0.

Both issues include a ready-made TEST_CASE repro; I added them verbatim to MaterialXTest/MaterialXCore/Document.cpp.

Validation

  • Built with MATERIALX_BUILD_TESTS=ON and MATERIALX_DYNAMIC_ANALYSIS=ON (ASan+UBSan), GCC 13, MATERIALX_BUILD_GEN_GLSL=ON (required by MaterialXTest's GenShader test), everything else generation/render-related off.
  • Red-before: reverted just the Version.cpp fix and reran the two new tests — both reproduce the exact runtime errors quoted in the issues (member call on null pointer of type 'struct element_type' for nulldref possible on Switch block #3067, reference binding to null pointer of type 'struct value_type' for Out of range access on empty swizzle #3068).
  • Green-after: with the fix restored, both new tests pass under ASan+UBSan.
  • Full local test binary (Core/Format/GenShader/GenGlsl/GenHw scope): 14427 assertions in 69 test cases, all passed, no new sanitizer trips anywhere else in that scope.
  • clang-format --dry-run clean on the lines this PR touches (pre-existing formatting drift elsewhere in Version.cpp/Document.cpp left untouched, per the contributing guide's "make unrelated changes in a separate PR").

I didn't build MATERIALX_BUILD_RENDER/the other shader-gen backends or the full CI matrix locally, since neither touched file is in that path.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@jstone-lucasfilm

Copy link
Copy Markdown
Member

Thanks for this proposal, @kshivam4781! Can you add a comment on both of the GitHub Issues that you're tackling, so that we can assign them to you? Additionally, see the instructions at #3069 (comment) on how to resolve the CLA authorization warnings, so that we can begin reviewing this PR.

@jstone-lucasfilm

Copy link
Copy Markdown
Member

Thanks for taking this on, @kshivam4781! This PR looks very close to the mark as it stands, though I have two suggestions that I believe would strengthen the proposal before we merge:

  1. The switch fix can be expressed as a single guard. Since ValueElement::getValue already returns a null pointer when no value is present, the existing hasValue check becomes redundant once we test the pointer itself, and the repeated whichValue && conditions can be removed:

    InputPtr which = node->getInput("which");
    ValuePtr whichValue = which ? which->getValue() : nullptr;
    if (whichValue)
    {
        if (whichValue->isA<int>() && whichValue->asA<int>() >= 5)
        {
            which->setValue(0);
        }
        else if (whichValue->isA<float>() && whichValue->asA<float>() >= 5)
        {
            which->setValue(0.0);
        }
    }
  2. Both of these issues are undefined behavior rather than thrown exceptions, so REQUIRE_NOTHROW would only catch a regression in our dynamic analysis build, and on other platforms the tests may pass or crash depending on the compiler. I'd suggest adding a check on the upgraded document in each test, which would give us coverage in every build and also document the fallback behavior that you've chosen. For the swizzle case, that might look like the following:

    mx::NodePtr node = doc->getNodeGraph("NG1")->getNode("swz1");
    REQUIRE(node->getCategory() == "constant");
    REQUIRE(node->getInput("value")->getValueString() == "0, 0, 0");

    For the switch case, a check that the which input retains its original value string would serve the same purpose.

One smaller note:

  • Since the two test cases were taken from separate issues, one uses an unescaped string literal for its document and the other uses escaped concatenation. I'd suggest unescaped strings for both, which are easier to read and match the direction we've taken in other recent PRs to MaterialX.

Overall this looks like a great first contribution, and with the refinements above, I believe it should be ready to merge.

Applies @jstone-lucasfilm's suggested single-guard refactor verbatim: since
ValueElement::getValue() already returns null when no value is present, the
separate hasValue() check and the repeated `whichValue &&` conditions on
each branch were redundant. Test coverage (Document.cpp, in a follow-up
commit) confirms behavior is unchanged.

Signed-off-by: Shivam <[email protected]>
Both new regression tests only asserted REQUIRE_NOTHROW, which catches a
thrown exception but not undefined behavior (null deref, out-of-range read)
-- exactly the shape of AcademySoftwareFoundation#3067/AcademySoftwareFoundation#3068, so only ASan/UBSan builds would have
caught a regression here, per @jstone-lucasfilm's review.

- switch null value: now also asserts the "which" input keeps its original
  ("abc") value string, confirming the fallback behavior chosen (leave an
  unparseable value untouched) rather than just "did not throw".
- swizzle empty value: now also asserts the upgraded node's category and
  value string (exact assertions from the review), confirming the empty/
  invalid channel name falls back to "0" per channel as intended.
- swizzle test's XML also switched from escaped string concatenation to an
  unescaped raw string literal, matching the switch test and the direction
  recent MaterialX PRs have taken.

Full local run: bin/MaterialXTest -- 74 test cases, 17815 assertions, all
passed (Debug build, MATERIALX_BUILD_TESTS=ON, GenGlsl on, other gen
backends/render/python off). clang-format --dry-run clean on every line
this commit and the prior one touch.

Signed-off-by: Shivam <[email protected]>
@kshivam4781

Copy link
Copy Markdown
Contributor Author

Thanks @jstone-lucasfilm! Pushed both suggestions:

  • a071eb8: collapsed the switch fix to the single guard you gave, verbatim.
  • 39ff484: both tests now assert real post-upgrade state instead of just REQUIRE_NOTHROW (the swizzle assertions are exactly what you suggested; the switch test checks that which keeps its original "abc" value string), and the swizzle test's document literal is now an unescaped raw string, matching the switch test.

Full local run: bin/MaterialXTest -- 74 test cases, 17815 assertions, all passed (Debug, MATERIALX_BUILD_TESTS=ON, GenGlsl on, other gen backends/render/python off). clang-format --dry-run clean on every line both commits touch.

@jstone-lucasfilm

Copy link
Copy Markdown
Member

Thanks for the quick turnaround, @kshivam4781. Both changes look good, and I appreciate the added assertions on the upgraded documents, which will give us coverage of these paths in all builds rather than only in dynamic analysis.

One small refinement before we merge: the new comment in the switch block ends by noting that the hasValue() check above is now redundant, but that check has been removed in this same change, so the comment now describes the history of the code rather than its current state. I'd suggest reducing it to a single line that states the property we rely on, along the lines of the following:

// getValue() returns null for both a missing and an unparseable value.

With that minor comment update, I believe this PR is ready to merge. Thanks again for taking this on!

kshivam4781 and others added 2 commits September 23, 2026 16:03
Per @jstone-lucasfilm: the comment still described the now-removed hasValue()
check as "redundant", which read as commentary on the code's own history rather
than its current state now that the check is gone. Reduced to a single line
stating the property the code relies on, exactly as suggested.

Rebuilt and reran the two upgrade regression tests plus the full Document suite
locally -- 3 test cases, 40 assertions, all passed. clang-format --dry-run clean
on the touched lines.

Signed-off-by: Shivam <[email protected]>

@jstone-lucasfilm jstone-lucasfilm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks @kshivam4781!

@jstone-lucasfilm
jstone-lucasfilm merged commit 47bffc8 into AcademySoftwareFoundation:main Sep 24, 2026
37 checks passed
warrenm pushed a commit to metal-by-example/MaterialX that referenced this pull request Sep 25, 2026
AcademySoftwareFoundation#3069)

Fixes AcademySoftwareFoundation#3067 and AcademySoftwareFoundation#3068.

Both are fuzz findings from @seang-ilm on the same code path: `Document::upgradeVersion()`'s 1.38-to-1.39 step for `switch` and `swizzle` nodes.

**AcademySoftwareFoundation#3067 (nulldref on switch):** `which->getValue()` returns null when the stored value string can't be parsed as the input's declared type (e.g. `value="abc"` on an `integer` input). The existing code called `whichValue->isA<int>()` without checking for that, so a bad `which` value crashes the upgrade instead of just being left alone. Fix adds a null check before both `isA`/`asA` calls.

**AcademySoftwareFoundation#3068 (out-of-range on swizzle):** when a `swizzle` node's `in` input has an empty value (`value=""`), `origValueTokens` ends up empty, but the fallback branch for an invalid/missing channel name unconditionally did `origValueTokens[0]`. Fix falls back to `"0"` when `origValueTokens` is empty, matching the existing convention in this same function of treating a missing/unparseable value as `0`.

Both issues include a ready-made `TEST_CASE` repro; I added them verbatim to `MaterialXTest/MaterialXCore/Document.cpp`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nulldref possible on Switch block

2 participants