Skip to content

Commit

Permalink
Check overflow in Loop_Integer
Browse files Browse the repository at this point in the history
or this code will cause an infinite loop:

>> a: 9223372036854775807
== 9223372036854775807

>> for b 0 a to integer! a / 2 [print b]

With this commit:
>> for b 0 a to integer! a / 2 [print b]
0
4611686018427387904
** Math error: math or number overflow
** Where: for
** Near: for b 0 a to integer! a / 2 [print b]
  • Loading branch information
zsx committed Aug 27, 2014
1 parent f869e00 commit 17200c7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/n-loop.c
Expand Up @@ -29,6 +29,7 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-int-funcs.h" //REB_I32_ADD_OF

This comment has been minimized.

Copy link
@earl

earl Aug 29, 2014

The comment is off: should rather mention REB_I64_ADD_OF.

This comment has been minimized.

Copy link
@zsx

zsx Aug 29, 2014

Author Owner

Thanks. Will fix later



/***********************************************************************
Expand Down Expand Up @@ -125,12 +126,16 @@

VAL_SET(var, REB_INTEGER);

for (; (incr > 0) ? start <= end : start >= end; start += incr) {
while ((incr > 0) ? start <= end : start >= end) {
VAL_INT64(var) = start;
result = Do_Blk(body, 0);
if (THROWN(result) && Check_Error(result) >= 0) break;
if (!IS_INTEGER(var)) Trap_Type(var);
start = VAL_INT64(var);

if (REB_I64_ADD_OF(start, incr, &start)) {
Trap0(RE_OVERFLOW);
}
}
}

Expand Down

0 comments on commit 17200c7

Please sign in to comment.