From 47c22608bfaac6b7125a9d0940c9514ca4bc2fb6 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Mon, 25 Mar 2024 22:43:48 +0800 Subject: [PATCH] bc: forbid negative array index (#521) * Negative array index is being caught when accessing a value (p instruction), but is not caught when assigning a value (P instruction) * Adapt existing guard code from 'p' instruction * For consistency with undefined-function error, replace YYERROR call with "last INSTR" * Only the 2nd-from-top ope_stack element is needed; fetch it with splice instead of pop+push dance for $value --- bin/bc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/bc b/bin/bc index bff8d7b1..400713a6 100755 --- a/bin/bc +++ b/bin/bc @@ -2459,10 +2459,14 @@ sub exec_stmt next INSTR; } elsif($_ eq 'P') { - - my $value = pop @ope_stack; - my $index = pop @ope_stack; - push @ope_stack, $value; + my $index = splice @ope_stack, -2, 1; # rval remains top of stack + if($index !~ /^\d+$/) { + print STDERR "Non-integer index $index for array\n"; + $return = 3; + @ope_stack = (); + @stmt_list=(); + last INSTR; + } push @ope_stack, $instr->[1] . '[]' . $index; next INSTR; @@ -2489,7 +2493,7 @@ sub exec_stmt $return = 3; @ope_stack = (); @stmt_list=(); - YYERROR; + last INSTR; } # debug {"p: $name, $idx.\n"};