From 56c3ab4ff86798aaf4f79d3d1a92041d05d202d6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 5 May 2022 20:04:51 +1000 Subject: [PATCH] Fix parsing of INT_MIN %arg Fixes #4601 --- src/command_manager.cc | 6 +++--- test/regression/4601-int-min-arg/cmd | 1 + test/regression/4601-int-min-arg/in | 1 + test/regression/4601-int-min-arg/out | 1 + test/regression/4601-int-min-arg/rc | 5 +++++ 5 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 test/regression/4601-int-min-arg/cmd create mode 100644 test/regression/4601-int-min-arg/in create mode 100644 test/regression/4601-int-min-arg/out create mode 100644 test/regression/4601-int-min-arg/rc 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 +}