Jump to content
msd

Integer Overflow - Exception

Recommended Posts

Hello developers,

 

I have one function which is working fine until Delphi 12 but now I get Integer Overflow Exception

 

procedure CalculateErrorCorrection(dest: integer);
var
  A: TInt;
  ALength, e, k, t1, t2, t3, LastE: Int64;
begin
  if (errorLevel < 0) or (errorLevel > 8) then
    errorLevel := 0;
  A := TInt(ERROR_LEVEL[errorLevel]);
  Alength := 2 shl errorLevel;
  for k := 0 to Alength - 1 do
    codewords[dest + k] := 0;
  lastE := Alength - 1;
  for k := 0 to lenCodewords - 1 do
  begin
    t1 := codewords[k] + codewords[dest];
    for e := 0 to LastE do
    begin
      t2 := (t1 * A[lastE - e]) mod _MOD;
      t3 := _MOD - t2;
      if e = LastE then
        codewords[dest + e] := t3 mod _MOD
      else
        codewords[dest + e] := ((codewords[dest + e + 1]) + t3) mod _MOD;
    end;
  end;
  for k := 0 to Alength - 1 do
    codewords[dest + k] := (_MOD - codewords[dest + k]) mod _MOD;
end;

it fail at line: t2 := (t1 * A[lastE - e]) mod _MOD;

every params were integer I change to Int64 but same error is there.

 

Thanks for the any help in advance...

Share this post


Link to post

Kinda hard to evaluate, given that there is zero example data for the various elements.

 

There was a change in 12 with regards to math exceptions.

If you add this code, it is supposed to behave like 11.x.

System.math.SetExceptionMask( [exPrecision, exUnderflow, exDenormalized]); // Enable exInvalidOp, exZeroDivide, and exOverflow exceptions

Ref. https://docwiki.embarcadero.com/RADStudio/Athens/en/Floating_Point_Operation_Exception_Masks


Not sure if this applies to integer math, though.

Share this post


Link to post

Make a minimal yet complète program that demonstrates the issue. 

 

Until you isolate the problem by doing this, everything is likely to be a waste of time with irrelevant speculation and guesswork. 

Edited by David Heffernan
  • Like 1

Share this post


Link to post
17 hours ago, msd said:

Alength := 2 shl errorLevel;

This may not do what you expect it to do. ALength may be int64 but the compiler will calculate the right-hand side using a smaller integer type into which both the constant 2 and errorlevel fit and only extend the result to int64 before storing it into Alength. So the calculation may well overflow the range of the type used for the calculation, and since D12 has enabled overflow and range checks by default you get an exception. Change the code to

 

Alength := int64(2) shl errorlevel;

to force the use of int64 for the calculation.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×