NetBSD Problem Report #8314

Received: (qmail 5308 invoked from network); 3 Sep 1999 07:39:48 -0000
Message-Id: <199909030739.QAA10380@icnmp9.icg.tnr.sharp.co.jp>
Date: Fri, 3 Sep 1999 16:39:18 +0900 (JST)
From: itohy@netbsd.org
Reply-To: itohy@netbsd.org,mjl@netbsd.org
To: gnats-bugs@gnats.netbsd.org
Subject: inet_addr(3) fails to report an error on incorrect octal number
X-Send-Pr-Version: 3.95

>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:    is
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Sep 03 00:50:01 +0000 1999
>Closed-Date:    Mon Nov 15 16:01:12 +0000 1999
>Last-Modified:  Mon Nov 15 16:01:46 +0000 1999
>Originator:     ITOH Yasufumi
>Release:        NetBSD 1.4K (Sept. 3, 1999)
>Organization:

>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)) {
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->analyzed 
State-Changed-By: is 
State-Changed-When: Wed Oct 27 05:35:15 PDT 1999 
State-Changed-Why:  
X/Open tells to return -1 when an error occurs. AFAICT, 8 and 9 are no 
valid octal digits. 
The proposed patch does not check for valid hexadecimal digits. I'll  
add a patch which checks both to this PR for review. 


Responsible-Changed-From-To: lib-bug-people->is 
Responsible-Changed-By: is 
Responsible-Changed-When: Wed Oct 27 05:35:15 PDT 1999 
Responsible-Changed-Why:  
Somebody has to do it. 

From: Ignatios Souvatzis <ignatios@cs.uni-bonn.de>
To: gnats-bugs@netbsd.org
Cc:  Subject: lib/8314
Date: Wed, 27 Oct 1999 14:43:28 +0200

 This patch makes our inet_addr() behave like on Solaris or SunOS.

 	-is


 Index: inet_pton.c
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libc/net/inet_pton.c,v
 retrieving revision 1.10
 diff -u -r1.10 inet_pton.c
 --- inet_pton.c	1999/08/17 17:50:27	1.10
 +++ inet_pton.c	1999/10/27 12:41:15
 @@ -95,6 +95,7 @@
  	u_char *dst;
  {
  	u_int32_t val;
 +	u_int digit;
  	int base, n;
  	unsigned char c;
  	u_int parts[4];
 @@ -119,11 +120,16 @@
  		}
  		for (;;) {
  			if (isdigit(c)) {
 -				val = (val * base) + (c - '0');
 +				digit = c - '0';
 +				if (digit >= base)
 +					break;
 +				val = (val * base) + digit;
  				c = *++src;
  			} else if (base == 16 && isxdigit(c)) {
 -				val = (val << 4) |
 -					(c + 10 - (islower(c) ? 'a' : 'A'));
 +				digit = c + 10 - (islower(c) ? 'a' : 'A');
 +				if (digit >= 16)
 +					break;
 +				val = (val << 4) | digit;
  				c = *++src;
  			} else
  			break;
State-Changed-From-To: analyzed->closed 
State-Changed-By: is 
State-Changed-When: Wed Nov 3 03:49:35 PST 1999 
State-Changed-Why:  
Patch applied to check for invalid octal and hex digits. 
State-Changed-From-To: closed->open 
State-Changed-By: mjl 
State-Changed-When: Fri Nov 5 03:21:58 PST 1999 
State-Changed-Why:  

Good first step, but what about "1.34768234.3.5" or the like. 
Is that ok, or should inet_addr() check for too big a number 
too? (IMO yes, but other peopl think it's a feature) 

State-Changed-From-To: open->analyzed 
State-Changed-By: is 
State-Changed-When: Thu Nov 11 03:04:01 PST 1999 
State-Changed-Why:  
The SUSV2 is very clear about this: 
Legal are: 
32bit 
8bit.24bit 
8bit.8bit.16bit 
8bit.8bit.8bit.8bit 

Not allowed are, e.g., 8bit.16bit.8bit, which our code accepts and turns 
into soemthingunpredictable, that is, not predicted by me.. 

I'll fix this alter today. 

Note that the single dotless number is allowed and interpreted as a 32bit  
value (converted to network byte order). This would be the format sometimes 
found in web links. 
State-Changed-From-To: analyzed->closed 
State-Changed-By: is 
State-Changed-When: Mon Nov 15 08:01:12 PST 1999 
State-Changed-Why:  
done. 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.