Module Name:    othersrc
Committed By:   lukem
Date:           Sat Sep 23 00:02:05 UTC 2023

Modified Files:
        othersrc/libexec/tnftpd/libnetbsd: inet_net_pton.c

Log Message:
sync lib/libc/inet/inet_net_pton.c 1.5

Functional changes since upstream 1.1:
- 1.3: fix lint issues
- 1.2: Check bits on each loop to prevent integer oveflow.

Retain tnftpd local changes:
- 1.3: don't use non-standard u_char u_int
- 1.1: only enable IPv6 ifdef INET6.

Comment out other unneeded upstream code to minimise differences.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 othersrc/libexec/tnftpd/libnetbsd/inet_net_pton.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/libexec/tnftpd/libnetbsd/inet_net_pton.c
diff -u othersrc/libexec/tnftpd/libnetbsd/inet_net_pton.c:1.4 othersrc/libexec/tnftpd/libnetbsd/inet_net_pton.c:1.5
--- othersrc/libexec/tnftpd/libnetbsd/inet_net_pton.c:1.4	Sun Sep 21 16:35:25 2008
+++ othersrc/libexec/tnftpd/libnetbsd/inet_net_pton.c	Sat Sep 23 00:02:05 2023
@@ -1,5 +1,7 @@
-/* $NetBSD: inet_net_pton.c,v 1.4 2008/09/21 16:35:25 lukem Exp $ */
-/* from	NetBSD: inet_net_pton.c,v 1.1 2004/05/20 23:13:02 christos Exp */
+/* $NetBSD: inet_net_pton.c,v 1.5 2023/09/23 00:02:05 lukem Exp $ */
+
+/* from:	NetBSD: inet_net_pton.c,v 1.5 2018/12/13 08:44:11 maya Exp */
+/* upstream:	lib/libc/inet/inet_net_pton.c */
 
 /*
  * Copyright (c) 1996,1999 by Internet Software Consortium.
@@ -19,14 +21,42 @@
  */
 
 #include "tnftpd.h"
+#undef INSIST
+#define INSIST(x) /* */
 
-
-#ifdef SPRINTF_CHAR
-# define SPRINTF(x) strlen(sprintf/**/x)
+#if 0
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static const char rcsid[] = "Id: inet_net_pton.c,v 1.4.2.1 2002/08/02 02:17:21 marka Exp ";
 #else
-# define SPRINTF(x) ((size_t)sprintf x)
+__RCSID(" NetBSD: inet_net_pton.c,v 1.5 2018/12/13 08:44:11 maya Exp  ");
+#endif
 #endif
 
+#include "port_before.h"
+
+#include "namespace.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <arpa/inet.h>
+
+#include <isc/assertions.h>
+#include <stddef.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "port_after.h"
+
+#ifdef __weak_alias
+__weak_alias(inet_net_pton,_inet_net_pton)
+#endif
+#endif
 
 /*
  * static int
@@ -45,12 +75,15 @@
  *	Paul Vixie (ISC), June 1996
  */
 static int
-inet_net_pton_ipv4( const char *src, unsigned char *dst, size_t size) {
+inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
+{
 	static const char xdigits[] = "0123456789abcdef";
 	static const char digits[] = "0123456789";
-	int n, ch, tmp = 0, dirty, bits;
+	int ch, dirty, bits;
+	ptrdiff_t n, tmp;
 	const unsigned char *odst = dst;
 
+	tmp = 0;
 	ch = *src++;
 	if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
 	    && isascii((unsigned char)(src[1]))
@@ -65,6 +98,7 @@ inet_net_pton_ipv4( const char *src, uns
 			if (isupper((unsigned char)ch))
 				ch = tolower((unsigned char)ch);
 			n = strchr(xdigits, ch) - xdigits;
+			INSIST(n >= 0 && n <= 15);
 			if (dirty == 0)
 				tmp = n;
 			else
@@ -87,13 +121,13 @@ inet_net_pton_ipv4( const char *src, uns
 			tmp = 0;
 			do {
 				n = strchr(digits, ch) - digits;
+				INSIST(n >= 0 && n <= 9);
 				tmp *= 10;
 				tmp += n;
 				if (tmp > 255)
 					goto enoent;
 			} while ((ch = *src++) != '\0' &&
-				 isascii((unsigned char)ch) &&
-				 isdigit((unsigned char)ch));
+				 isascii((unsigned char)ch) && isdigit((unsigned char)ch));
 			if (size-- == 0)
 				goto emsgsize;
 			*dst++ = (unsigned char) tmp;
@@ -102,8 +136,7 @@ inet_net_pton_ipv4( const char *src, uns
 			if (ch != '.')
 				goto enoent;
 			ch = *src++;
-			if (!isascii((unsigned char)ch) ||
-			    !isdigit((unsigned char)ch))
+			if (!isascii((unsigned char)ch) || !isdigit((unsigned char)ch))
 				goto enoent;
 		}
 	} else
@@ -117,14 +150,15 @@ inet_net_pton_ipv4( const char *src, uns
 		bits = 0;
 		do {
 			n = strchr(digits, ch) - digits;
+			INSIST(n >= 0 && n <= 9);
 			bits *= 10;
-			bits += n;
+			bits += (int)n;
+			if (bits > 32)
+				goto emsgsize;
 		} while ((ch = *src++) != '\0' && isascii((unsigned char)ch)
 		    && isdigit((unsigned char)ch));
 		if (ch != '\0')
 			goto enoent;
-		if (bits > 32)
-			goto emsgsize;
 	}
 
 	/* Firey death and destruction unless we prefetched EOS. */
@@ -148,7 +182,7 @@ inet_net_pton_ipv4( const char *src, uns
 			bits = 8;
 		/* If imputed mask is narrower than specified octets, widen. */
 		if (bits >= 8 && bits < ((dst - odst) * 8))
-			bits = (dst - odst) * 8;
+			bits = (int)(dst - odst) * 8;
 	}
 	/* Extend network to cover the actual mask. */
 	while (bits > ((dst - odst) * 8)) {
@@ -169,7 +203,8 @@ inet_net_pton_ipv4( const char *src, uns
 
 #ifdef INET6
 static int
-getbits(const char *src, int *bitsp) {
+getbits(const char *src, int *bitsp)
+{
 	static const char digits[] = "0123456789";
 	int n;
 	int val;
@@ -185,7 +220,7 @@ getbits(const char *src, int *bitsp) {
 			if (n++ != 0 && val == 0)	/* no leading zeros */
 				return (0);
 			val *= 10;
-			val += (pch - digits);
+			val += (int)(pch - digits);
 			if (val > 128)			/* range */
 				return (0);
 			continue;
@@ -199,7 +234,8 @@ getbits(const char *src, int *bitsp) {
 }
 
 static int
-getv4(const char *src, unsigned char *dst, int *bitsp) {
+getv4(const char *src, unsigned char *dst, int *bitsp)
+{
 	static const char digits[] = "0123456789";
 	unsigned char *odst = dst;
 	int n;
@@ -216,7 +252,7 @@ getv4(const char *src, unsigned char *ds
 			if (n++ != 0 && val == 0)	/* no leading zeros */
 				return (0);
 			val *= 10;
-			val += (pch - digits);
+			val += (int)(pch - digits);
 			if (val > 255)			/* range */
 				return (0);
 			continue;
@@ -242,7 +278,8 @@ getv4(const char *src, unsigned char *ds
 }
 
 static int
-inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size) {
+inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
+{
 	static const char xdigits_l[] = "0123456789abcdef",
 			  xdigits_u[] = "0123456789ABCDEF";
 	unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
@@ -275,7 +312,7 @@ inet_net_pton_ipv6(const char *src, unsi
 			pch = strchr((xdigits = xdigits_u), ch);
 		if (pch != NULL) {
 			val <<= 4;
-			val |= (pch - xdigits);
+			val |= (int)(pch - xdigits);
 			if (++digits > 4)
 				goto enoent;
 			saw_xdigit = 1;
@@ -331,7 +368,7 @@ inet_net_pton_ipv6(const char *src, unsi
 		 * Since some memmove()'s erroneously fail to handle
 		 * overlapping regions, we'll do the shift by hand.
 		 */
-		const int n = tp - colonp;
+		const ptrdiff_t n = tp - colonp;
 		int i;
 
 		if (tp == endp)
@@ -375,7 +412,8 @@ inet_net_pton_ipv6(const char *src, unsi
  *	Paul Vixie (ISC), June 1996
  */
 int
-inet_net_pton(int af, const char *src, void *dst, size_t size) {
+inet_net_pton(int af, const char *src, void *dst, size_t size)
+{
 	switch (af) {
 	case AF_INET:
 		return (inet_net_pton_ipv4(src, dst, size));

Reply via email to