Module Name:    src
Committed By:   martin
Date:           Fri Aug 23 18:50:16 UTC 2024

Modified Files:
        src/common/lib/libc/stdlib [netbsd-9]: _strtoi.h
        src/lib/libc/stdlib [netbsd-9]: Makefile.inc strtoi.3 strtonum.c
        src/tests/lib/libc/stdlib [netbsd-9]: t_strtoi.c
Removed Files:
        src/lib/libc/stdlib [netbsd-9]: strtou.3

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1870):

        common/lib/libc/stdlib/_strtoi.h: revision 1.3
        lib/libc/stdlib/strtou.3: revision 1.8
        lib/libc/stdlib/strtonum.c: revision 1.7
        lib/libc/stdlib/Makefile.inc: revision 1.97
        lib/libc/stdlib/strtoi.3: revision 1.8
        lib/libc/stdlib/strtoi.3: revision 1.9
        lib/libc/stdlib/strtou.3: file removal
        tests/lib/libc/stdlib/t_strtoi.c: revision 1.3

PR/57828: Alejandro Colomar: Prioritize test for ERANGE before testing for
fully consuming the string. Adjust strtonum(3) to behave as before. Document
the order of the tests and sync the man pages (I should really autogenerate
one of the two man pages...)
generate strtou.3 from strtoi.3, grammar police

Add range tests with trailing characters.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.2.20.1 src/common/lib/libc/stdlib/_strtoi.h
cvs rdiff -u -r1.95 -r1.95.2.1 src/lib/libc/stdlib/Makefile.inc
cvs rdiff -u -r1.7 -r1.7.8.1 src/lib/libc/stdlib/strtoi.3
cvs rdiff -u -r1.6 -r1.6.2.1 src/lib/libc/stdlib/strtonum.c
cvs rdiff -u -r1.7 -r0 src/lib/libc/stdlib/strtou.3
cvs rdiff -u -r1.2 -r1.2.12.1 src/tests/lib/libc/stdlib/t_strtoi.c

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

Modified files:

Index: src/common/lib/libc/stdlib/_strtoi.h
diff -u src/common/lib/libc/stdlib/_strtoi.h:1.2 src/common/lib/libc/stdlib/_strtoi.h:1.2.20.1
--- src/common/lib/libc/stdlib/_strtoi.h:1.2	Sun Jan 18 17:55:22 2015
+++ src/common/lib/libc/stdlib/_strtoi.h	Fri Aug 23 18:50:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: _strtoi.h,v 1.2 2015/01/18 17:55:22 christos Exp $	*/
+/*	$NetBSD: _strtoi.h,v 1.2.20.1 2024/08/23 18:50:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -100,26 +100,26 @@ INT_FUNCNAME(_int_, _FUNCNAME, _l)(const
 	errno = serrno;
 #endif
 
-	if (*rstatus == 0) {
-		/* No digits were found */
-		if (nptr == *endptr)
-			*rstatus = ECANCELED;
-		/* There are further characters after number */
-		else if (**endptr != '\0')
-			*rstatus = ENOTSUP;
-	}
+	/* No digits were found */
+	if (*rstatus == 0 && nptr == *endptr)
+		*rstatus = ECANCELED;
 
 	if (im < lo) {
 		if (*rstatus == 0)
 			*rstatus = ERANGE;
 		return lo;
 	}
+
 	if (im > hi) {
 		if (*rstatus == 0)
 			*rstatus = ERANGE;
 		return hi;
 	}
 
+	/* There are further characters after number */
+	if (*rstatus == 0 && **endptr != '\0')
+		*rstatus = ENOTSUP;
+
 	return im;
 }
 

Index: src/lib/libc/stdlib/Makefile.inc
diff -u src/lib/libc/stdlib/Makefile.inc:1.95 src/lib/libc/stdlib/Makefile.inc:1.95.2.1
--- src/lib/libc/stdlib/Makefile.inc:1.95	Mon Mar  4 17:30:33 2019
+++ src/lib/libc/stdlib/Makefile.inc	Fri Aug 23 18:50:16 2024
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.95 2019/03/04 17:30:33 christos Exp $
+#	$NetBSD: Makefile.inc,v 1.95.2.1 2024/08/23 18:50:16 martin Exp $
 #	from: @(#)Makefile.inc	8.3 (Berkeley) 2/4/95
 
 # stdlib sources
@@ -62,6 +62,12 @@ MAN+=	a64l.3 abort.3 abs.3 alloca.3 atex
 	tsearch.3 \
 	unlockpt.3
 
+strtou.3: strtoi.3
+	${_MKTARGET_CREATE}
+	${TOOL_SED} -e s/strtoi/strtou/g -e s/STRTOI/STRTOU/g \
+	    -e s/intmax_t/uintmax_t/g -e s/%jd/%ju/g ${.ALLSRC} > ${.TARGET}
+CLEANFILES += strtou.3
+
 MLINKS+=a64l.3 l64a.3
 MLINKS+=a64l.3 l64a_r.3
 MLINKS+=abs.3 labs.3 \

Index: src/lib/libc/stdlib/strtoi.3
diff -u src/lib/libc/stdlib/strtoi.3:1.7 src/lib/libc/stdlib/strtoi.3:1.7.8.1
--- src/lib/libc/stdlib/strtoi.3:1.7	Mon Jul  3 21:32:50 2017
+++ src/lib/libc/stdlib/strtoi.3	Fri Aug 23 18:50:16 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: strtoi.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
+.\"	$NetBSD: strtoi.3,v 1.7.8.1 2024/08/23 18:50:16 martin Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -36,12 +36,12 @@
 .\" Created by Kamil Rytarowski, based on ID:
 .\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp
 .\"
-.Dd November 13, 2015
+.Dd January 20, 2024
 .Dt STRTOI 3
 .Os
 .Sh NAME
 .Nm strtoi
-.Nd convert string value to an intmax_t integer
+.Nd convert a string value to an intmax_t integer
 .Sh LIBRARY
 .Lb libc
 .Sh SYNOPSIS
@@ -58,8 +58,7 @@
 .Sh DESCRIPTION
 The
 .Fn strtoi
-function
-converts the string in
+function converts the string in
 .Fa nptr
 to an
 .Ft intmax_t
@@ -117,10 +116,11 @@ is taken as 10 (decimal) unless the next
 .Ql 0 ,
 in which case it is taken as 8 (octal).
 .Pp
-The remainder of the string is converted to a
+The remainder of the string is converted to an
 .Em intmax_t
 value in the obvious manner,
-stopping at the first character which is not a valid digit
+stopping at the end of the string
+or at the first character which is not a valid digit
 in the given base.
 (In bases above 10, the letter
 .Ql A
@@ -201,6 +201,12 @@ or the range given was invalid, i.e.
 >
 .Fa hi .
 .El
+.Pp
+The range check is more important than the unconverted characters check,
+and it is performed first.
+If a program needs to know if there were unconverted characters when an
+out of range number has been provided, it needs to supply and test
+.Fa endptr.
 .Sh SEE ALSO
 .Xr atof 3 ,
 .Xr atoi 3 ,

Index: src/lib/libc/stdlib/strtonum.c
diff -u src/lib/libc/stdlib/strtonum.c:1.6 src/lib/libc/stdlib/strtonum.c:1.6.2.1
--- src/lib/libc/stdlib/strtonum.c:1.6	Thu Dec  6 06:29:56 2018
+++ src/lib/libc/stdlib/strtonum.c	Fri Aug 23 18:50:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: strtonum.c,v 1.6 2018/12/06 06:29:56 kamil Exp $	*/
+/*	$NetBSD: strtonum.c,v 1.6.2.1 2024/08/23 18:50:16 martin Exp $	*/
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: strtonum.c,v 1.6 2018/12/06 06:29:56 kamil Exp $");
+__RCSID("$NetBSD: strtonum.c,v 1.6.2.1 2024/08/23 18:50:16 martin Exp $");
 
 #include "namespace.h"
 
@@ -46,26 +46,33 @@ strtonum(const char *nptr, long long min
 	int e;
 	long long rv;
 	const char *resp;
+	char *eptr;
 
 	if (errstr == NULL)
 		errstr = &resp;
 
-	if (minval > maxval) {
-		*errstr = "invalid";
-		return 0;
-	}
+	if (minval > maxval)
+		goto out;
 
-	rv = (long long)strtoi(nptr, NULL, 10, minval, maxval, &e);
+	rv = (long long)strtoi(nptr, &eptr, 10, minval, maxval, &e);
 
-	if (e == 0) {
+	switch (e) {
+	case 0:
 		*errstr = NULL;
 		return rv;
+	case ECANCELED:
+	case ENOTSUP:
+		goto out;
+	case ERANGE:
+		if (*eptr)
+			goto out;
+		*errstr = rv == maxval ? "too large" : "too small";
+		return 0;
+	default:
+		abort();
 	}
 
-	if (e == ERANGE)
-		*errstr = (rv == maxval ? "too large" : "too small");
-	else
-		*errstr = "invalid";
-
+out:
+	*errstr = "invalid";
 	return 0;
 }

Index: src/tests/lib/libc/stdlib/t_strtoi.c
diff -u src/tests/lib/libc/stdlib/t_strtoi.c:1.2 src/tests/lib/libc/stdlib/t_strtoi.c:1.2.12.1
--- src/tests/lib/libc/stdlib/t_strtoi.c:1.2	Fri Apr 28 19:01:01 2017
+++ src/tests/lib/libc/stdlib/t_strtoi.c	Fri Aug 23 18:50:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_strtoi.c,v 1.2 2017/04/28 19:01:01 kamil Exp $	*/
+/*	$NetBSD: t_strtoi.c,v 1.2.12.1 2024/08/23 18:50:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_strtoi.c,v 1.2 2017/04/28 19:01:01 kamil Exp $");
+__RCSID("$NetBSD: t_strtoi.c,v 1.2.12.1 2024/08/23 18:50:16 martin Exp $");
 
 #include <atf-c.h>
 #include <errno.h>
@@ -235,6 +235,38 @@ ATF_TC_BODY(strtoi_range, tc)
 	}
 }
 
+ATF_TC(strtoi_range_trail);
+ATF_TC_HEAD(strtoi_range_trail, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test ERANGE from strtoi(3) "
+		"with trailing characters");
+}
+
+ATF_TC_BODY(strtoi_range_trail, tc)
+{
+	struct test t[] = {
+		{ "11x", 9, 10, "x", 0,	9, ERANGE },
+		{ " -3y", -2, 10, "y", -2, 1, ERANGE },
+	};
+
+	intmax_t rv;
+	char *end;
+	int e;
+	size_t i;
+
+	for (i = 0; i < __arraycount(t); i++) {
+
+		errno = 0;
+		rv = strtoi(t[i].str, &end, t[i].base, t[i].lo, t[i].hi, &e);
+
+		if (errno != 0)
+			atf_tc_fail("strtoi(3) changed errno to %d ('%s')",
+			            e, strerror(e));
+
+		check(&t[i], rv, end, e);
+	}
+}
+
 ATF_TC(strtoi_signed);
 ATF_TC_HEAD(strtoi_signed, tc)
 {
@@ -298,6 +330,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, strtoi_base);
 	ATF_TP_ADD_TC(tp, strtoi_case);
 	ATF_TP_ADD_TC(tp, strtoi_range);
+	ATF_TP_ADD_TC(tp, strtoi_range_trail);
 	ATF_TP_ADD_TC(tp, strtoi_signed);
 
 	return atf_no_error();

Reply via email to