Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-11

inetaux.pas

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Mills

Hi,

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?

thank you
James

Marco van de Voort
to James Mills
Show quoted text 30 lines
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