[fpc-pascal]inetaux.pas
Marco van de Voort
marcov at stack.nl
Mon Nov 4 11:36:12 CET 2002
> Could someone help me out here. I didn't even write this pascal file,
> yet it throws 'Range Check Errors' in the program particularly with this
> ip address: 200.34.32.196
>
> If it chucks an error with that address, the function mustn't work very
> well, but I can't seem to see what's wrong with it.
> function StrToAddr(s : String) : LongInt;
> var
> r, i, p, c : LongInt;
> t : String;
> begin
> r := 0;
> for i := 0 to 3 do
> begin
> p := Pos('.', s);
> if p = 0 then p := Length(s) + 1;
> if p <= 1 then exit;
> t := Copy(s, 1, p - 1);
> Delete(s, 1, p);
> Val(t, p, c);
> if (c <> 0) or (p < 0) or (p > 255) then exit;
> r := r or p shl (i * 8);
> end;
> StrToAddr := r;
> end;
>
>
> Does someone have a fixed version of this function, or can someone fix
> it please?
Simple, in the last iteration you shift a 8 bit number by 3*8=24. So if the
last value is larger than 127, the longint (which is 31-bit + 1 sign)
overflows.
So the problem is r (and the expression with the shl) being calculated in
longints. Use a 32-bit _unsigned_ value, like cardinal.
Try to make r and p cardinal
More information about the fpc-pascal
mailing list