Skip to content

Commit

Permalink
[pmc] fix splice overflows with negative count
Browse files Browse the repository at this point in the history
Fixes GH #766.
Throw illegal argument with negative count arguments for now.

TODO: Implement support for negative count argument for splice.
Add more splice methods.
  • Loading branch information
Reini Urban committed Nov 10, 2014
1 parent 16091e9 commit ec7e1ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/pmc/fixedpmcarray.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,19 @@ of this array.
*/

VTABLE void splice(PMC *value, INTVAL offset, INTVAL count) :manual_wb {
const INTVAL elems0 = VTABLE_elements(INTERP, SELF);
/* start from end? */
if (offset < 0)
offset += elems0;
if (offset < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("illegal splice offset"));
if (count < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("illegal argument"));
if (count + offset > PMC_size(SELF))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
_("FixedPMCArray: index out of bounds!"));
_("index out of bounds"));

for (count--; count >= 0; --count) {
VTABLE_set_pmc_keyed_int(INTERP, SELF, offset + count, value);
Expand Down
9 changes: 7 additions & 2 deletions src/pmc/resizablepmcarray.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,15 @@ Note that the C<from> PMC can be of any of the various array types.
/* start from end? */
if (offset < 0)
offset += elems0;

if (offset < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"illegal splice offset\n");
_("illegal splice offset"));
if (count < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("illegal argument"));
if (count + offset > elems0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("index out of bounds"));

/* number of elements to the right of the splice (the "tail") */
tail = elems0 - offset - count;
Expand Down
5 changes: 4 additions & 1 deletion src/pmc/resizablestringarray.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,10 @@ everything via the VTABLE api.

if (offset < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"illegal splice offset\n");
_("illegal splice offset"));
if (count < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("illegal argument"));

/* shrink the array */
if (shift < 0) {
Expand Down

0 comments on commit ec7e1ac

Please sign in to comment.