diff --git a/src/command_manager.cc b/src/command_manager.cc index b8784093..266b832d 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -382,10 +382,10 @@ void expand_token(Token&& token, const Context& context, const ShellContext& she return set_target(params); } - const int arg = str_to_int(content)-1; - if (arg < 0) + const int arg = str_to_int(content); + if (arg < 1) throw runtime_error("invalid argument index"); - return set_target(arg < params.size() ? params[arg] : String{}); + return set_target(arg <= params.size() ? params[arg-1] : String{}); } case Token::Type::FileExpand: return set_target(read_file(content)); diff --git a/test/regression/4601-int-min-arg/cmd b/test/regression/4601-int-min-arg/cmd new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/regression/4601-int-min-arg/cmd @@ -0,0 +1 @@ + diff --git a/test/regression/4601-int-min-arg/in b/test/regression/4601-int-min-arg/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/regression/4601-int-min-arg/in @@ -0,0 +1 @@ + diff --git a/test/regression/4601-int-min-arg/out b/test/regression/4601-int-min-arg/out new file mode 100644 index 00000000..9766475a --- /dev/null +++ b/test/regression/4601-int-min-arg/out @@ -0,0 +1 @@ +ok diff --git a/test/regression/4601-int-min-arg/rc b/test/regression/4601-int-min-arg/rc new file mode 100644 index 00000000..b9a641ac --- /dev/null +++ b/test/regression/4601-int-min-arg/rc @@ -0,0 +1,5 @@ +try %{ + nop %arg{-2147483648} +} catch %{ + exec iok +}