Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Overflow in source code

ASta
2009-04-16
2013-04-19
  • ASta
    ASta
    2009-04-16

    -- FreeGeometry.pas --
      function DistanceToLine(...): TFloatType;
      begin
        ...
        Result := 1e10;
        ...
      end;
      ...
      var
        Dist: Integer
        ...
        Dist := Round(DistanceToLine(...); // Overflow error (1e10 large then integer)
    ----
    Patch:
    -- FreeGeometry.pas --
      function DistanceToLine(P1, P2:TPoint; X, Y: Integer; var Parameter: TFloatType): Integer;
      const
        VeryVeryLargeDistance = 1000000;
      var
        Pt: T2DCoordinate;
      begin
        if (P1.X<0) and (P2.X<0) then
          Result := VeryVeryLargeDistance
        else
          if (P1.X>Screen.Width) and (P2.X>Screen.width) then
            Result := VeryVeryLargeDistance
          else
            if (P1.Y<0) and (P2.Y<0) then
              Result := VeryVeryLargeDistance
            else
              if (P1.Y>Screen.Height) and (P2.Y>Screen.Height) then
                Result := VeryVeryLargeDistance
              else
                if (P2.X=P1.X) and (P1.Y=P2.Y) then
                  Result := Round(Sqrt(Sqr(P1.X-X)+Sqr(P1.Y-Y)))
                else
                begin
                  Parameter:=((X-P1.X)*(P2.X-P1.X)+(Y-P1.Y)*(P2.Y-P1.Y))/(sqr(P2.X-P1.X)+Sqr(P2.Y-P1.Y));
                  if (Parameter>=0) and (Parameter<=1) then
                  begin
                    Pt.X:=P1.X+Parameter*(P2.X-P1.X);
                    Pt.Y:=P1.Y+Parameter*(P2.Y-P1.Y);
                    if (Pt.X>=0) and (Pt.Y>=0) and (Pt.X<=Screen.Width) and (Pt.Y<=Screen.Height) then
                      Result := Round(Sqrt(Sqr(Pt.X-X)+Sqr(Pt.Y-Y)))
                    else
                      Result := VeryVeryLargeDistance;
                  end
                  else
                    Result := VeryVeryLargeDistance;
                end;
      end;{DistanceToLine}

     
      ... := DistanceToLine(...); // was: ... := Round(DistanceToLine(...);
      ...
    -- --

    -- FreeShipUnit.pas --
      ...
     
      ... := FreeGeometry.DistanceToLine(...); // was: ... := Round(DistanceToLine(...);
      ...
    ----

    Comments/ideas?