AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Überlauf bei int64

Ein Thema von Hobbycoder · begonnen am 22. Feb 2025 · letzter Beitrag vom 25. Feb 2025
Antwort Antwort
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
10.054 Beiträge
 
Delphi 12 Athens
 
#1

AW: Überlauf bei int64

  Alt 22. Feb 2025, 22:42
Oder schlicht so:
Delphi-Quellcode:
uses
  Winapi.Winsock;

function IPToCardinal(const AIP: string): Cardinal;
begin
  Result := ntohl(inet_addr(PAnsiChar(AnsiString(AIP))));
end;
Alternativ, wenn es manuell passieren soll (ohne Fehlerbehandlung):
Delphi-Quellcode:
function IPToCardinal2(const AIP: string): Cardinal;
var
  Value, Octet: Cardinal;
  i, ShlValue: Integer;
begin
  Result := 0;
  Value := 0;
  ShlValue := 24;
  for i := 1 to Length(AIP) do
  begin
    if AIP[i] = '.then
    begin
      Result := Result or (Value shl ShlValue);
      Value := 0;
      ShlValue := ShlValue - 8;
    end
    else
      Value := Value * 10 + (Ord(AIP[i]) - Ord('0'));
  end;
  Result := Result or Value;
end;
Sebastian Jänicke
AppCentral

Geändert von jaenicke (22. Feb 2025 um 22:46 Uhr)
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
457 Beiträge
 
#2

AW: Überlauf bei int64

  Alt 23. Feb 2025, 08: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
Rollo62

Registriert seit: 15. Mär 2007
4.240 Beiträge
 
Delphi 12 Athens
 
#3

AW: Überlauf bei int64

  Alt 23. Feb 2025, 09:43
TLDR;
Es sollten doch die Octets auch auf Überlauf überprüft werden, also so ungefähr:

Edit: Ok, DeddyH hatte die Prüfung auch schon vorgeschlagen ...

Code:
function IP2Int(const value: string): Int64;
var
  parts: TArray<string>;
  i, octet: Integer;
begin
  Result := 0;
  parts := value.Split(['.']);
  if Length(parts) <> 4 then
    Exit;
  for i := 0 to 3 do
  begin
    if not TryStrToInt(parts[i], octet) then
      Exit;
    if (octet < 0) or (octet > 255) then  //<== Check this
      Exit;
    Result := (Result shl 8) + octet;
  end;
end;
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:02 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz