Subject: lib/8314: inet_addr(3) fails to report an error on incorrect octal number
To: None <gnats-bugs@gnats.netbsd.org>
From: None <itohy@netbsd.org>
List: netbsd-bugs
Date: 09/03/1999 01:09:28
>Number:         8314
>Category:       lib
>Synopsis:       inet_addr(3) fails to report an error on incorrect octal number
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people (Library Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Sep  3 00:50:01 1999
>Last-Modified:
>Originator:     ITOH Yasufumi
>Organization:
	
>Release:        NetBSD 1.4K (Sept. 3, 1999)
>Environment:
System: NetBSD pino.my.domain 1.4K NetBSD 1.4K (PINO) #17: Sun Aug 29 16:38:37 JST 1999 itohy@pino.my.domain:/usr/src/sys/arch/i386/compile/PINO i386


>Description:
	inet_addr(3) doesn't treat incorrect octal number (start with '0'
	but have '8' or '9') as an error.

	I think it should be treated as an error (inet_addr(3) of SunOS 4
	reports an error), but I'm not sure this is a bug.
	Can anyone check the standards?

	# By the way, MS Windows 9x has the same problem. :-)

>How-To-Repeat:
	% /sbin/ping 099.099.099.099
	PING 099.099.099.099 (81.81.81.81): 56 data bytes
	^C
	% cc itest.c	# see below
	% ./a.out 099.099.099.099
	inet_addr(099.099.099.099) = 0x51515151, 81.81.81.81
	%

	In this example, "099.099.099.099" is treated as 81.81.81.81 .

itest.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>

main(argc, argv)
	int argc;
	char *argv[];
{
	unsigned long adr;

	adr = ntohl(inet_addr(argv[1]));
	printf("inet_addr(%s) = 0x%lx, %d.%d.%d.%d\n",
	    argv[1], adr, (int)(adr >> 24), (int)(adr >> 16) & 0xff,
	    (int)(adr >> 8) & 0xff, (int)adr & 0xff);

	return 0;
}

>Fix:
	Apply this patch.

diff -uF^[a-zA-Z_][a-z 	A-Z0-9_]*(.*[^;]$ lib/libc/net/inet_pton.c.orig lib/libc/net/inet_pton.c
--- lib/libc/net/inet_pton.c.orig	Wed Aug 18 14:29:32 1999
+++ lib/libc/net/inet_pton.c	Fri Sep  3 16:16:40 1999
@@ -119,6 +119,9 @@ inet_pton4(src, dst)
 		}
 		for (;;) {
 			if (isdigit(c)) {
+				/* 8 and 9 are not octal */
+				if (base == 8 && (c == '8' || c == '9'))
+					break;
 				val = (val * base) + (c - '0');
 				c = *++src;
 			} else if (base == 16 && isxdigit(c)) {
>Audit-Trail:
>Unformatted: