Fix ruby/ruby#9562
This commit is contained in:
Nobuyoshi Nakada 2024-01-17 11:26:12 +09:00
Родитель 4216880261
Коммит 6215b5ba98
2 изменённых файлов: 12 добавлений и 2 удалений

4
ruby.c
Просмотреть файл

@ -1369,14 +1369,14 @@ proc_long_options(ruby_cmdline_options_t *opt, const char *s, long argc, char **
(((allow_envopt) || !envopt) ? (void)0 : \
rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
# define need_argument(name, s, needs_arg, next_arg) \
((*(s) ? !*++(s) : (next_arg) && (!argc || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
((*(s) ? !*++(s) : (next_arg) && (argc <= 1 || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
rb_raise(rb_eRuntimeError, "missing argument for --" name) \
: (void)0)
# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
(strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
(s[n] != '-' || s[n+1]) ? \
(s[n] != '-' || (s[n] && s[n+1])) ? \
(check_envopt(name, (allow_envopt)), s += n, \
need_argument(name, s, needs_arg, next_arg), 1) : 0)

Просмотреть файл

@ -541,6 +541,16 @@ class TestRubyOptions < Test::Unit::TestCase
/invalid name for global variable - -# \(NameError\)/)
end
def test_option_missing_argument
assert_in_out_err(%w(-0 --enable), "", [], /missing argument for --enable/)
assert_in_out_err(%w(-0 --disable), "", [], /missing argument for --disable/)
assert_in_out_err(%w(-0 --dump), "", [], /missing argument for --dump/)
assert_in_out_err(%w(-0 --encoding), "", [], /missing argument for --encoding/)
assert_in_out_err(%w(-0 --external-encoding), "", [], /missing argument for --external-encoding/)
assert_in_out_err(%w(-0 --internal-encoding), "", [], /missing argument for --internal-encoding/)
assert_in_out_err(%w(-0 --backtrace-limit), "", [], /missing argument for --backtrace-limit/)
end
def test_assignment_in_conditional
Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t|
t.puts "if a = 1"