[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+}


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

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

$ ./ip4_neg
ip is valid:

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.


$ 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