[Bug #21018] Show invalid command line option more properly

This commit is contained in:
Nobuyoshi Nakada 2025-01-08 21:35:48 +09:00
parent d44a41d814
commit dfe6b7c02e
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-01-09 10:55:49 +00:00
2 changed files with 25 additions and 5 deletions

22
ruby.c
View File

@ -1721,11 +1721,27 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt)
if (!s[1])
break;
default:
default: {
rb_encoding *enc = IF_UTF8_PATH(rb_utf8_encoding(), rb_locale_encoding());
const char *e = s + strlen(s);
int r = rb_enc_precise_mbclen(s, e, enc);
unsigned int c = (unsigned char)*s;
if (r > 0) {
c = rb_enc_mbc_to_codepoint(s, e, enc);
if (ONIGENC_IS_CODE_GRAPH(enc, c) ||
((s = ruby_escaped_char(c)) != 0 &&
(r = (int)strlen(s), /* 3 at most */ 1))) {
rb_enc_raise(enc, rb_eRuntimeError,
"invalid option -%.*s (-h will show valid options)",
r, s);
}
}
rb_raise(rb_eRuntimeError,
"invalid option -%c (-h will show valid options)",
(int)(unsigned char)*s);
"invalid option -\\x%.2x (-h will show valid options)",
c);
goto switch_end;
}
noenvopt:
/* "EIdvwWrKU" only */

View File

@ -441,11 +441,15 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err(%W(-\r -e) + [""], "", [], [])
assert_in_out_err(%W(-\rx), "", [], /invalid option -[\r\n] \(-h will show valid options\) \(RuntimeError\)/)
assert_in_out_err(%W(-\rx), "", [], /invalid option -\\r \(-h will show valid options\) \(RuntimeError\)/)
assert_in_out_err(%W(-\x01), "", [], /invalid option -\x01 \(-h will show valid options\) \(RuntimeError\)/)
assert_in_out_err(%W(-\x01), "", [], /invalid option -\\x01 \(-h will show valid options\) \(RuntimeError\)/)
assert_in_out_err(%w(-Z), "", [], /invalid option -Z \(-h will show valid options\) \(RuntimeError\)/)
# On some platforms, langinfo returns ANSI_X3.4-1968 when LC_ALL=C and fall backs to UTF-8.
# Anyway only printable chars should be printed.
assert_in_out_err(%W(-\u{1f608}), "", [], /invalid option -(\\xf0|\u{1f608}) \(-h will show valid options\) \(RuntimeError\)/)
end
def test_rubyopt