[fpc-pascal] StrToHostAddr in sockets unit accepts negative octets

Noel Duffy noelduffy at xtra.co.nz
Sun Apr 19 02:38:45 CEST 2020


Running fpc 3.0.4 on Fedora 30.

The StrToHostAddr function in the sockets unit accepts negative octets
in the source ip address. Consider this program:

program ip4_neg;

{$mode objfpc}{$H+}

uses
   sockets;

var
   ip4_addr: in_addr;
begin
   ip4_addr := StrToHostAddr('172.-16.32.14');
   if ip4_addr.s_addr = 0 then
     writeln('invalid ip.')
   else
     writeln('ip is valid: ' + HostAddrToStr(ip4_addr));
end.

This program parses an invalid ip address. But when run:

$ ./ip4_neg
ip is valid: 172.240.32.14

This seems to me to be a bug. I tested this in Python using its
socket.inet_aton (a wrapper around the C library function) and also
directly in C and both returned an error for this address.

E.g,

$ python
Python 2.7.17 (default, Oct 21 2019, 17:20:57)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import socket
 >>> socket.inet_aton('172.-16.32.14')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
socket.error: illegal IP address string passed to inet_aton
 >>>




More information about the fpc-pascal mailing list