Thema: Delphi Überlauf bei int64

Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
386 Beiträge
 
#6

AW: Überlauf bei int64

  Alt Heute, 09:22
Hi,

Away from the answers above, your code doesn't validate the input for valid IP address and will always result in something, so i suggest to replace the converting part and building with something like this:
Delphi-Quellcode:
    {if TryStrToInt(v1, i1) then
    if TryStrToInt(v2, i2) then
      if TryStrToInt(v3, i3) then
        if TryStrToInt(v4, i4) then
        begin
          Result := i1 * 16777216 + // Corrected from 16777246
                  i2 * 65536 +
                  i3 * 256 +
                  i4;
        end ;  }

  i1 := StrToIntDef(v1, -1);
  i2 := StrToIntDef(v2, -1);
  i3 := StrToIntDef(v3, -1);
  i4 := StrToIntDef(v4, -1);

  if Cardinal(i1 or i2 or i3 or i4) > 255 then // bit manipulation if any has higher bit than 8 then invalid (this includes negatives)
    begin
      raise Exception.Create('Invalid IP address');
    end;

  Result := i1 shl 24 or i2 shl 16 or i3 shl 8 or i4;
  // or
  //Result := i1 shl 24 + i2 shl 16 + i3 shl 8 + i4;
Kas
  Mit Zitat antworten Zitat