Skip to content

Commit

Permalink
bc: forbid negative array index (#521)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mknos committed Mar 25, 2024
1 parent be51b31 commit 47c2260
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions bin/bc
Expand Up @@ -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;

Expand All @@ -2489,7 +2493,7 @@ sub exec_stmt
$return = 3;
@ope_stack = ();
@stmt_list=();
YYERROR;
last INSTR;
}

# debug {"p: $name, $idx.\n"};
Expand Down

0 comments on commit 47c2260

Please sign in to comment.