Fix integer underflow in all_mutually_exclusive for empty subschemas#1028
Open
danieleades wants to merge 1 commit into
Open
Fix integer underflow in all_mutually_exclusive for empty subschemas#1028danieleades wants to merge 1 commit into
danieleades wants to merge 1 commit into
Conversation
An empty subschema list (e.g. from a degenerate "anyOf": []) caused `len - 1` to underflow: a panic in debug builds and an effectively unbounded iteration in release builds. Return true for fewer than two subschemas since mutual exclusivity is vacuously satisfied.
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.
Problem
all_mutually_exclusiveintypify-impl/src/util.rscomputes(0..len - 1)over the subschema list. When the list is empty,len - 1underflows: this panics in debug builds and, in release builds, wraps tousize::MAXso the pair iteration effectively hangs. This is reachable from a degenerate but parseable input schema containing"anyOf": [], viaconvert_any_of.Fix
Guard at the top of the function: with fewer than two subschemas there are no pairs to compare, so mutual exclusivity is vacuously true (this preserves the existing behavior for a single subschema, which also produced no pairs). Added a unit test for the empty slice and a conversion-level test that an
"anyOf": []schema converts without panicking.Trade-offs
An empty
anyOfis arguably invalid input, so an alternative would be to reject it with an error during conversion. This change keeps the current behavior (the pipeline already handles it downstream) and only removes the underflow.