[M3devel] Integer overflow

Tony Hosking hosking at cs.purdue.edu
Sun Jan 10 05:45:29 CET 2010


Even under the interpretation for INC that yields:

VAR s: CARDINAL := LAST(INTEGER);
BEGIN WITH x = s DO x := VAL(ORD(x) + 1, CARDINAL) END; END;

the compiler currently does not insert a bounds check, because it reasons that the bound for ORD(x) + 1 is [0+1, LAST(INTEGER)], so the result is always assignable to CARDINAL.

In reality, the compiler should presumably assume that because ORD(x) + 1 might overflow if ORD(x) = LAST(INTEGER) then the bound for the expression ORD(x)+1 is actually the same as INTEGER: [FIRST(INTEGER),LAST(INTEGER)].  So, because this is larger than the range for CARDINAL it really needs to insert a bounds check on the conversion to CARDINAL.

On 9 Jan 2010, at 23:33, Tony Hosking wrote:

> So, in my experiments with range checking on integers, I have been playing with the overflow case:
> 
> VAR s: CARDINAL := LAST(INTEGER);
> BEGIN INC(s) END;
> 
> Should this fail at runtime with a range check error?
> 
> The language definition says that integer overflow may or may not be checked, depending on the implementation.  (See FloatMode.i3).
> 
> If it is not checked, is it reasonable that s takes on the value FIRST(INTEGER)?
> 
> Antony Hosking | Associate Professor | Computer Science | Purdue University
> 305 N. University Street | West Lafayette | IN 47907 | USA
> Office +1 765 494 6001 | Mobile +1 765 427 5484
> 
> 
> 
> 




More information about the M3devel mailing list