Repository navigation
Do not raise ArgumentError on an argument with invalid bytes - #163
Open
ihamzatarar wants to merge 1 commit into
Open
ihamzatarar wants to merge 1 commit into
ihamzatarar wants to merge 1 commit into
Conversation
Regexp#=== raises ArgumentError for a string that is not valid in its encoding, and parse_in_order matches every argument against the option patterns before it can tell whether the argument is an option. A single bad byte in ARGV therefore took the parser down, including on arguments optparse would have returned untouched, such as a file name from a system using another encoding. Switch#parse_arg had the same problem when matching a switch's pattern against its value. Match a binary copy at both sites. Option syntax is ASCII, so the structure still parses and the bytes are passed on as they arrived. Only arguments that raise today are affected. String#rindex is the reason parse_arg needs this too: on an invalid UTF-8 string it does not raise, it fails to find a substring the string does contain, so converting back before that line would turn the crash into a spurious InvalidArgument.
This branch has not been deployed
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.
Fixes #161.
Regexp#===raisesArgumentErroron a string that is not valid in its encoding, andparse_in_ordermatches every argument against the long and short option patterns before it can tell whether the argument is an option at all. So one bad byte anywhere inARGVtakes the parser down, including on an argument optparse would have returned untouched.There are two sites, not one. The second is
Switch#parse_arg, which matches the switch's own pattern against the value.Change
Each site works on
arg.bwhen the argument is not valid in its encoding. Option syntax is ASCII, so the structure still parses, and the bytes are passed on as they came in.The only arguments this touches are ones that raise today, so no program that currently works can observe a difference.
Behaviour
With
-n/--name=NAMEand-v/--verbosedefined, on Ruby 3.4.6:--bogusInvalidOptionInvalidOptioncaf\xE9.txtArgumentError--name=\xFFArgumentErrornameset to\xFF--name \xFFArgumentErrornameset to\xFF-n\xFFArgumentErrornameset to\xFF--verbose caf\xE9.txtArgumentErrorverbose, and the file name in the restcafé.txtWhy pass the bytes through rather than raise
ParseErrorThe issue asks for a
ParseError, which would be an improvement over a backtrace, and I am happy to do that instead if you prefer. Passing the bytes through seemed closer to what Ruby already does: it hands arguments toARGVas the bytes it was given, without validating them, andFile.opentakes them back. A file name from another system is a legitimate argument, so refusing it is a harder rule than optparse needs.Either way this fixes the reported crash in
ARGV.options { ... }and inri, since both rescueParseErrorand neither rescuesArgumentError.One thing worth recording, which is why the second site needs the same treatment: on an invalid UTF-8 string,
String#rindexdoes not raise, it simply fails to find a substring the string does contain.parse_argrelies onarg.rindex(s, 0)right after the match, so matching in binary and converting back before that line turns the crash into a spuriousInvalidArgument.Tests
test/optparse/test_invalid_encoding.rb, six cases. Five fail on master; the sixth asserts an unknown option still raisesInvalidOption, and passes either way, so the change cannot swallow real parse errors.Suite on Ruby 3.4.6, run standalone:
Both errors are the same two,
test_help_pagerandtest_ver, which needassert_in_out_errandassert_warningfrom ruby/ruby's test helpers and are not available when the suite runs outside the Ruby tree.