Fix ValueError/IndexError crash in SafeLoader for malformed numeric scalars (#898)#920
Open
Nicolas0315 wants to merge 1 commit intoyaml:mainfrom
Open
Conversation
…calars
Wrap construct_yaml_int and construct_yaml_float in try/except to
catch ValueError and IndexError from malformed inputs like '0x_',
'!!float +_', and '!!float 1::3'. These previously bubbled up as
uncaught exceptions instead of ConstructorError.
After underscore removal, values like '0x_' become '0x' which causes
int('', 16) to raise ValueError. Similarly, '!!float +_' becomes an
empty string after removing the sign, causing IndexError on value[0].
The fix delegates to private _construct_yaml_int/_construct_yaml_float
methods and wraps them with proper ConstructorError conversion.
Fixes yaml#898
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.
Summary
Fix uncaught
ValueError/IndexErrorinSafeLoaderwhen parsing malformed numeric scalars like0x_,!!float +_, and!!float 1::3.Root Cause
construct_yaml_intandconstruct_yaml_floatstrip underscores from the scalar value before parsing, but don't handle the case where stripping leaves an empty or invalid string:replace('_', '')0x_0xint('', 16)→ValueError0b_0bint('', 2)→ValueError!!float +_+→ ``(empty after sign strip)value[0]→IndexError!!float 1::31::3float('')(empty split part) →ValueErrorFix
Wrap the parsing logic in try/except to convert
ValueErrorandIndexErrorintoConstructorError, which is the expected exception type for invalid YAML values. This matches the behavior of other constructors in PyYAML (e.g.construct_yaml_binary).Testing
Fixes #898