Skip to content

Commit

Permalink
ATRONIX/FIX: 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]

(cherry picked from commit 17200c7)
(cherry picked from commit 2b5f9a8)

Fixes: metaeducation/rebol-issues#2168
  • Loading branch information
Oldes committed Jun 25, 2018
1 parent a831c5b commit 4966f0b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/n-loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
***********************************************************************/

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


/***********************************************************************
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 4966f0b

Please sign in to comment.