psych icon indicating copy to clipboard operation
psych copied to clipboard

Error Parsing Integer with Leading/Trailing Underscore

Open xokocodo opened this issue 5 years ago • 5 comments

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> ```

xokocodo avatar Mar 18 '20 18:03 xokocodo

:+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.

maknz avatar Mar 19 '20 21:03 maknz

I submitted a fix for it: https://github.com/ruby/psych/issues/442

casperisfine avatar Apr 06 '20 09:04 casperisfine

Woops, wrong issue number, sorry: https://github.com/ruby/psych/pull/445

casperisfine avatar Apr 06 '20 09:04 casperisfine

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

ccutrer avatar Jun 22 '21 19:06 ccutrer