Laddok Does Not Prevent Overflows

Product Version(s): 3.20 3.3x
Operating System:   MS-DOS
Flags: ENDUSER | TAR05457
Last Modified: 30-SEP-1988    ArticleIdent: Q10390

Problem:

The no-overflow addition function for integer4 numbers (laddok) does
not handle adding 1 to the maximum positive integer4 number
(2,147,483,647). The program crashes when this procedure is attempted.
The correct result for laddok(2147483647,1) is the minimum negative
integer4 number (-2,147,483,647). The calculation laddok(2147483647,2)
does produce the result -2,147,483,647. The following is a short
example code:

   program RollOver(input, output);
   VAR k: integer4;
   FUNCTION LADDOK(A, B: integer4; var c: integer4): boolean; extern;
   begin
   writeln('Rollover Test');
   writeln;

   if laddok(2147483647, 2, k) then
   WRITE('NO OVERFLOW: ')
   else
   WRITE('OVERFLOW: ');
   writeln('Result = ',k);

   if laddok(2147483647, 1, k) then
   WRITE('NO OVERFLOW: ')
   else
   WRITE('OVERFLOW: ');
   writeln('Result = ',k)
   end.

Response:

The behavior of the example program is entirely correct; your
diagnosis is incorrect.

The result of laddok(2147483647,1,k) is 16#80000000, which is not a
valid integer4 value. Laddok permits you to carry out this add without
getting a fatal run-time error. However, there is no way to keep
write/writeln from discovering that variable "k" has this invalid
value in the subsequent "writeln('Result = ',k);". It is this attempt
to write out the illegal value that is causing the run-time error. You
can get the same error by setting "k := 16#80000000" and then trying a
"write(k)".
