Repository navigation
Fix null/out-of-range access in the switch and swizzle version upgrade - #3069
Conversation
…Foundation#3067, AcademySoftwareFoundation#3068) Refactor conditions to check for null values before type checks for 'whichValue'. Update fallback logic for invalid channel names. Signed-off-by: Shivam <[email protected]>
|
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. |
|
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:
One smaller note:
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]>
|
Thanks @jstone-lucasfilm! Pushed both suggestions:
Full local run: |
|
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 With that minor comment update, I believe this PR is ready to merge. Thanks again for taking this on! |
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
left a comment
There was a problem hiding this comment.
Great work, thanks @kshivam4781!
47bffc8
into
AcademySoftwareFoundation:main
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`.
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 forswitchandswizzlenodes.#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 anintegerinput). The existing code calledwhichValue->isA<int>()without checking for that, so a badwhichvalue crashes the upgrade instead of just being left alone. Fix adds a null check before bothisA/asAcalls.#3068 (out-of-range on swizzle): when a
swizzlenode'sininput has an empty value (value=""),origValueTokensends up empty, but the fallback branch for an invalid/missing channel name unconditionally didorigValueTokens[0]. Fix falls back to"0"whenorigValueTokensis empty, matching the existing convention in this same function of treating a missing/unparseable value as0.Both issues include a ready-made
TEST_CASErepro; I added them verbatim toMaterialXTest/MaterialXCore/Document.cpp.Validation
MATERIALX_BUILD_TESTS=ONandMATERIALX_DYNAMIC_ANALYSIS=ON(ASan+UBSan), GCC 13,MATERIALX_BUILD_GEN_GLSL=ON(required byMaterialXTest's GenShader test), everything else generation/render-related off.Version.cppfix 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).clang-format --dry-runclean on the lines this PR touches (pre-existing formatting drift elsewhere inVersion.cpp/Document.cppleft 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.