Skip to content

Raise from skip when the position is an invalid byte - #229

Open
adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/invalid-byte-skip
Open

adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/invalid-byte-skip

Conversation

@adityawrk

Copy link
Copy Markdown

Fixes #223.

skip returned nil when the position was not a character head. That is the right result in the middle of a valid multibyte character, and test_scan_at_non_character_boundary covers it. An invalid byte is not that case. In strscan 3.1.6 the regexp engine raised, and a caller that moves forward only after a match loops if the answer is nil.

On current master, before this change:

Shift_JIS "/\xFF" at byte 1: nil
UTF-8     "/\xFF" at byte 1: ArgumentError
UTF-16LE  "/a"    at byte 1: nil   # interior of a valid character

After it, the Shift_JIS position raises ArgumentError: invalid byte sequence in Shift_JIS, matching 3.1.6 and the UTF-8 case. The interior of a valid character still returns nil, including a Shift_JIS two-byte character and the existing UTF-16 example.

bundle exec rake test loads the compiled extension: 178 tests, 1141 assertions.

A Shift_JIS string positioned on 0xFF made skip return nil.
The same call raises ArgumentError in strscan 3.1.6, and a
caller that advances only after a match can loop. A position
inside a valid two-byte character still misses.
The scan fast-path returned nil whenever the position was not
a character head. That is right for the interior of a valid
multibyte character, and wrong for an invalid byte: the regexp
engine used to raise ArgumentError there. Only a position
inside a complete character returns nil now.

Fixes ruby#223

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StringScanner#skip returns nil inside an invalid character in non-UTF-8 strings (was an exception in 3.1.6)

1 participant