Branch: Tag:

2014-07-15

2014-07-15 10:56:33 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Optimizer: Fixed bug in foreach with ranges.

The foreach range optimizer didn't handle negative start ranges,
which would cause foreach to index the array out of bounds.

Fixes [bug 7216 (#7216)].

1708:    /* Optimize foreach(x[start..],y). */    do_docode (CAR(range), DO_NOT_COPY_TOPLEVEL);    do_docode (CDR(arr), DO_NOT_COPY|DO_LVALUE); +  if ((low->token == F_CONSTANT) && (TYPEOF(low->u.sval) == PIKE_T_INT)) { +  if (low->u.sval.u.integer < 0) { +  emit0(F_CONST0); +  goto foreach_arg_pushed; +  }    do_docode (CAR(low), DO_NOT_COPY);    goto foreach_arg_pushed;    } -  +  do_docode (CAR(low), DO_NOT_COPY); +  tmp1 = alloc_label(); +  emit0(F_DUP); +  emit0(F_CONST0); +  do_jump(F_BRANCH_WHEN_GE, tmp1); +  /* The value is negative. replace it with zero. */ +  emit0(F_POP_VALUE); +  emit0(F_CONST0); +  low_insert_label(DO_NOT_WARN((INT32)tmp1)); +  goto foreach_arg_pushed;    } -  +  }    do_docode(arr,DO_NOT_COPY);    emit0(F_CONST0);    current_stack_depth++;