psych
psych copied to clipboard
Error Parsing Integer with Leading/Trailing Underscore
ScalarScanner.parse_int uses Integer() to parse items that match the INTEGER regex. Strings with leading/trailing underscores will fail to parse, even though they match the regex.
Example:
=> 1234
irb(main):012:0> Integer("_1234")
ArgumentError (invalid value for Integer(): "_1234")
irb(main):013:0> Integer("1234_")
ArgumentError (invalid value for Integer(): "1234_")
irb(main):014:0> ```
:+1:, we're encountering this issue too. The bug was introduced between versions 3.0.2 and 3.1.0.
For example, we're hitting this with a value like 1234________5678, which should be (and is) treated like a string in 3.0.2.
Our workaround for now is to downgrade to 3.0.2 from Ruby 2.7's default 3.1.0.
I submitted a fix for it: https://github.com/ruby/psych/issues/442
Woops, wrong issue number, sorry: https://github.com/ruby/psych/pull/445
note that even with #438 merged in, there is still a regression with the string "0x_". It passes the regex validation, then the _ is stripped, and just "0x" is passed to Integer(), which then raises an error. My quick monkey-patch is
def parse_int(string)
super
rescue ArgumentError
string
end