NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/49429: Import strtonum(3)



>Number:         49429
>Category:       lib
>Synopsis:       Import strtonum(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sat Nov 29 03:00:00 +0000 2014
>Originator:     Kamil Rytarowski
>Release:        NetBSD-current
>Organization:
>Environment:
NetBSD chieftec 7.99.1 NetBSD 7.99.1 (GENERIC) #0: Fri Nov  7 22:04:53 CET 2014  kamil@chieftec:/tmp/netbsd-tmp/sys/arch/amd64/compile/GENERIC amd64
>Description:
Hello,

OpenBSD standardized in its eco-system i.e. strtonum(3) as a yet another function to change literal string to a number.

strtonum(3) became a BSD standard (OpenBSD since 3.6 A.D. 2004, FreeBSD since 6.1 A.D. 2004)
reallocarray(3) was introduced in OpenBSD 5.6 (A.D. 2014)
strtonum(3) is in Bitrig etc

strtonum(3) is not just de facto standard in BSDs, it's also available in GNU/Linux's GLIBC (declared as _GNU_SOURCE!)

This function is still missing in NetBSD, reusing code from other systems is followed by adding strtonum(3) clones all over again is considered harmful.

Please merge OpenBSD's implementation with NetBSD.


Discussion at: http://mail-index.netbsd.org/tech-userlevel/2014/11/24/msg008812.html

A patch attached with this PR is placing strtonum(3) under _NETBSD_SOURCE guards.

I'm not sure whether I need to add anything to compat, tools or other pieces of the software. strtonum(3) is highly desired as a part of libnbcompat

A possible conflict with:
uint64_t strtonum(const char *str, char **nptr) defined in.... ZFS!

/public/netbsd-current/src/external/cddl/osnet/dist/uts/common/fs/zfs/

I request from a more experienced developer to take it, review adjust and merge with the NetBSD-current sources. Thank you in advance!
>How-To-Repeat:
N/A
>Fix:
From 821ad1682c654f81bf89dd0b9fb072ad37477e42 Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54%gmx.com@localhost>
Date: Fri, 28 Nov 2014 22:35:13 +0000
Subject: Add strtonum(3) as _NETBSD_SOURCE

---
 stdlib.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/stdlib.h b/stdlib.h
index 697605a..1fe5649 100644
--- a/stdlib.h
+++ b/stdlib.h
@@ -339,6 +339,7 @@ size_t	shquotev(int, char * const *, char *, size_t);
 
 #if defined(_NETBSD_SOURCE)
 qdiv_t	 qdiv(quad_t, quad_t);
+long long strtonum(const char *, long long, long long, const char **);
 #endif
 
 #if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
-- 
2.1.0




From 7ed46d1ea5611ae28e92eb6669e18d3505b6516d Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54%gmx.com@localhost>
Date: Fri, 28 Nov 2014 22:16:49 +0000
Subject: Add strtonum.*

---
 sets/lists/comp/mi | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sets/lists/comp/mi b/sets/lists/comp/mi
index 77e7474..37269ff 100644
--- a/sets/lists/comp/mi
+++ b/sets/lists/comp/mi
@@ -9181,6 +9181,7 @@
 ./usr/share/man/cat3/strtol.0			comp-c-catman		.cat
 ./usr/share/man/cat3/strtold.0			comp-c-catman		.cat
 ./usr/share/man/cat3/strtoll.0			comp-c-catman		.cat
+./usr/share/man/cat3/strtonum.0			comp-c-catman		.cat
 ./usr/share/man/cat3/strtoq.0			comp-c-catman		.cat
 ./usr/share/man/cat3/strtoul.0			comp-c-catman		.cat
 ./usr/share/man/cat3/strtoull.0			comp-c-catman		.cat
@@ -15973,6 +15974,7 @@
 ./usr/share/man/html3/strtol.html		comp-c-htmlman		html
 ./usr/share/man/html3/strtold.html		comp-c-htmlman		html
 ./usr/share/man/html3/strtoll.html		comp-c-htmlman		html
+./usr/share/man/html3/strtonum.html		comp-c-htmlman		html
 ./usr/share/man/html3/strtoq.html		comp-c-htmlman		html
 ./usr/share/man/html3/strtoul.html		comp-c-htmlman		html
 ./usr/share/man/html3/strtoull.html		comp-c-htmlman		html
@@ -22801,6 +22803,7 @@
 ./usr/share/man/man3/strtol.3			comp-c-man		.man
 ./usr/share/man/man3/strtold.3			comp-c-man		.man
 ./usr/share/man/man3/strtoll.3			comp-c-man		.man
+./usr/share/man/man3/strtonum.3			comp-c-man		.man
 ./usr/share/man/man3/strtoq.3			comp-c-man		.man
 ./usr/share/man/man3/strtoul.3			comp-c-man		.man
 ./usr/share/man/man3/strtoull.3			comp-c-man		.man
-- 
2.1.0




From b291bd1b1ee6c63887748a06f464047ce248d2d4 Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54%gmx.com@localhost>
Date: Fri, 28 Nov 2014 22:03:01 +0000
Subject: [PATCH 1/2] Add verbatim copy of strtonum.[3c] from OpenBSD-current

---
 libc/stdlib/strtonum.3 | 152 +++++++++++++++++++++++++++++++++++++++++++++++++
 libc/stdlib/strtonum.c |  65 +++++++++++++++++++++
 2 files changed, 217 insertions(+)
 create mode 100644 libc/stdlib/strtonum.3
 create mode 100644 libc/stdlib/strtonum.c

diff --git a/libc/stdlib/strtonum.3 b/libc/stdlib/strtonum.3
new file mode 100644
index 0000000..ed638cd
--- /dev/null
+++ b/libc/stdlib/strtonum.3
@@ -0,0 +1,152 @@
+.\" $OpenBSD: strtonum.3,v 1.17 2013/08/14 06:32:28 jmc Exp $
+.\"
+.\" Copyright (c) 2004 Ted Unangst
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: August 14 2013 $
+.Dt STRTONUM 3
+.Os
+.Sh NAME
+.Nm strtonum
+.Nd reliably convert string value to an integer
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft long long
+.Fo strtonum
+.Fa "const char *nptr"
+.Fa "long long minval"
+.Fa "long long maxval"
+.Fa "const char **errstr"
+.Fc
+.Sh DESCRIPTION
+The
+.Fn strtonum
+function converts the string in
+.Fa nptr
+to a
+.Li long long
+value.
+The
+.Fn strtonum
+function was designed to facilitate safe, robust programming
+and overcome the shortcomings of the
+.Xr atoi 3
+and
+.Xr strtol 3
+family of interfaces.
+.Pp
+The string may begin with an arbitrary amount of whitespace
+(as determined by
+.Xr isspace 3 )
+followed by a single optional
+.Ql +
+or
+.Ql -
+sign.
+.Pp
+The remainder of the string is converted to a
+.Li long long
+value according to base 10.
+.Pp
+The value obtained is then checked against the provided
+.Fa minval
+and
+.Fa maxval
+bounds.
+If
+.Fa errstr
+is non-null,
+.Fn strtonum
+stores an error string in
+.Fa *errstr
+indicating the failure.
+.Sh RETURN VALUES
+The
+.Fn strtonum
+function returns the result of the conversion,
+unless the value would exceed the provided bounds or is invalid.
+On error, 0 is returned,
+.Va errno
+is set, and
+.Fa errstr
+will point to an error message.
+.Fa *errstr
+will be set to
+.Dv NULL
+on success;
+this fact can be used to differentiate
+a successful return of 0 from an error.
+.Sh EXAMPLES
+Using
+.Fn strtonum
+correctly is meant to be simpler than the alternative functions.
+.Bd -literal -offset indent
+int iterations;
+const char *errstr;
+
+iterations = strtonum(optarg, 1, 64, &errstr);
+if (errstr)
+	errx(1, "number of iterations is %s: %s", errstr, optarg);
+.Ed
+.Pp
+The above example will guarantee that the value of iterations is between
+1 and 64 (inclusive).
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ERANGE
+The given string was out of range.
+.It Bq Er EINVAL
+The given string did not consist solely of digit characters.
+.It Bq Er EINVAL
+.Ar minval
+was larger than
+.Ar maxval .
+.El
+.Pp
+If an error occurs,
+.Fa errstr
+will be set to one of the following strings:
+.Pp
+.Bl -tag -width "too largeXX" -compact
+.It Qq too large
+The result was larger than the provided maximum value.
+.It Qq too small
+The result was smaller than the provided minimum value.
+.It Qq invalid
+The string did not consist solely of digit characters.
+.El
+.Sh SEE ALSO
+.Xr atof 3 ,
+.Xr atoi 3 ,
+.Xr atol 3 ,
+.Xr atoll 3 ,
+.Xr sscanf 3 ,
+.Xr strtod 3 ,
+.Xr strtol 3 ,
+.Xr strtoul 3
+.Sh STANDARDS
+.Fn strtonum
+is an
+.Ox
+extension.
+The existing alternatives, such as
+.Xr atoi 3
+and
+.Xr strtol 3 ,
+are either impossible or difficult to use safely.
+.Sh HISTORY
+The
+.Fn strtonum
+function first appeared in
+.Ox 3.6 .
diff --git a/libc/stdlib/strtonum.c b/libc/stdlib/strtonum.c
new file mode 100644
index 0000000..1aeee34
--- /dev/null
+++ b/libc/stdlib/strtonum.c
@@ -0,0 +1,65 @@
+/*	$OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $	*/
+
+/*
+ * Copyright (c) 2004 Ted Unangst and Todd Miller
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#define	INVALID		1
+#define	TOOSMALL	2
+#define	TOOLARGE	3
+
+long long
+strtonum(const char *numstr, long long minval, long long maxval,
+    const char **errstrp)
+{
+	long long ll = 0;
+	int error = 0;
+	char *ep;
+	struct errval {
+		const char *errstr;
+		int err;
+	} ev[4] = {
+		{ NULL,		0 },
+		{ "invalid",	EINVAL },
+		{ "too small",	ERANGE },
+		{ "too large",	ERANGE },
+	};
+
+	ev[0].err = errno;
+	errno = 0;
+	if (minval > maxval) {
+		error = INVALID;
+	} else {
+		ll = strtoll(numstr, &ep, 10);
+		if (numstr == ep || *ep != '\0')
+			error = INVALID;
+		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
+			error = TOOSMALL;
+		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
+			error = TOOLARGE;
+	}
+	if (errstrp != NULL)
+		*errstrp = ev[error].errstr;
+	errno = ev[error].err;
+	if (error)
+		ll = 0;
+
+	return (ll);
+}
-- 
2.1.0




From 27d83f1b8b189dc72f048339f673be50d57a074c Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54%gmx.com@localhost>
Date: Fri, 28 Nov 2014 22:04:47 +0000
Subject: [PATCH 2/2] Add strtonum.[c3] to Makefile

---
 libc/stdlib/Makefile.inc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/stdlib/Makefile.inc b/libc/stdlib/Makefile.inc
index 54e078d..73c220c 100644
--- a/libc/stdlib/Makefile.inc
+++ b/libc/stdlib/Makefile.inc
@@ -15,7 +15,7 @@ SRCS+=	_env.c _rand48.c \
 	seed48.c setenv.c srand48.c strsuftoll.c \
 	strtoimax.c strtol.c strtoll.c strtoq.c strtoul.c strtoull.c \
 	strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c \
-	unsetenv.c strfmon.c
+	unsetenv.c strfmon.c strtonum.c
 
 # These are often replaced by MD .S versions
 SRCS+=	abs.c labs.c llabs.c imaxabs.c
@@ -50,7 +50,7 @@ MAN+=	a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
 	posix_memalign.3 posix_openpt.3 ptsname.3 \
 	qabs.3 qdiv.3 quick_exit.3 qsort.3 \
 	radixsort.3 rand48.3 rand.3 random.3 \
-	strfmon.3 strsuftoll.3 strtod.3 strtol.3 strtoul.3 system.3 \
+	strfmon.3 strsuftoll.3 strtod.3 strtol.3 strtonum.3 strtoul.3 system.3 \
 	tsearch.3 \
 	unlockpt.3
 
-- 
2.1.0



Home | Main Index | Thread Index | Old Index