Fix OneHotEncoder segfault due to missing input shape validation#7302
Merged
gramalingam merged 3 commits intomainfrom Sep 19, 2025
Merged
Fix OneHotEncoder segfault due to missing input shape validation#7302gramalingam merged 3 commits intomainfrom
gramalingam merged 3 commits intomainfrom
Conversation
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot
AI
changed the title
[WIP]
Fix OneHotEncoder segfault due to missing input shape validation
Sep 13, 2025
onnx.helper.check_model segfaults due to bug in OneHotEncoder's shape inference
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7302 +/- ##
=======================================
Coverage 54.35% 54.35%
=======================================
Files 511 511
Lines 31837 31837
Branches 2850 2850
=======================================
Hits 17304 17304
Misses 13755 13755
Partials 778 778 ☔ View full report in Codecov by Sentry. |
justinchuby
approved these changes
Sep 15, 2025
Member
|
@cbourjau I tested locally and this seems to be the correct fix. Could you review? I also wonder how to prevent this type of errors systematically. |
Contributor
|
The "Reproduction" snippet does not inspire confidence, but the solution looks like the same check that is done in other operators, too. I don't see an easy way to prevent this error systematically in the current code base :/ |
cbourjau
approved these changes
Sep 19, 2025
gramalingam
approved these changes
Sep 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The OneHotEncoder operator in
ai.onnx.mlwas causing segmentation faults when used with custom operators that don't provide complete shape inference information. This occurred because the shape inference function directly accessed the input tensor shape without validating that the input type information was available.Problem
The crash occurred in the OneHotEncoder shape inference function at this line:
When a custom operator (like
CustomIdentityin the example below) precedes OneHotEncoder and doesn't provide shape information,ctx.getInputType(0)could returnnullptror the input might not have a tensor type, leading to a segmentation fault.Reproduction
Solution
Added proper input validation using
hasNInputShapes(ctx, 1)before accessing the input shape, following the established pattern used by other operators in the codebase:This function safely checks that:
Impact
Fixes #7298.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.