Registriert seit: 11. Aug 2012
Ort: Essen
1.660 Beiträge
Delphi 10.2 Tokyo Professional
|
AW: Überlauf bei int64
Gestern, 18:34
Ich habe das mal Claude vorgelegt und der hat noch einen Fehler gefunden:
Zitat:
The most significant error is in the first multiplier: 16777246. This number appears to be incorrect. For converting IP addresses to integers, the correct multiplier should be 16777216 (which is 256³). The current value of 16777246 is off by 30, which can lead to incorrect results.
Und er gibt noch folgende Ratschläge:
Zitat:
To make the function more robust, you might also want to: - Add range checking to ensure each octet is between 0 and 255
- Add error handling for malformed IP addresses
- Consider using Cardinal or UInt32 instead of Int64 since IPv4 addresses fit in 32 bits
Alles durchaus sinnvoll.
Das eigentliche Problem hat er allerdings in seinem "korrigierten" Code nicht gelöst:
Delphi-Quellcode:
function IP2Int(value: string): Int64;
var
s, v1, v2, v3, v4: string;
i1, i2, i3, i4: Integer;
begin
Result := 0;
s := value;
if Pos('.', s) > 0 then
begin
v1 := Copy(s, 1, pos('.', s)-1);
s := Copy(s, Pos('.', s)+1, Length(s));
if Pos('.', s) > 0 then
begin
v2 := Copy(s, 1, pos('.', s)-1);
s := Copy(s, Pos('.', s)+1, Length(s));
if Pos('.', s) > 0 then
begin
v3 := Copy(s, 1, pos('.', s)-1);
v4 := Copy(s, Pos('.', s)+1, Length(s));
end;
end;
end;
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;
end;
Der Integer Overflow dürfte immer noch auftreten.
Thomas Mueller
Geändert von dummzeuch (Heute um 12:09 Uhr)
|
|
Zitat
|