diff options
author | millert <> | 2004-08-03 19:38:01 +0000 |
---|---|---|
committer | millert <> | 2004-08-03 19:38:01 +0000 |
commit | ea13120433234de14cfef017f9b900c5d4e3874e (patch) | |
tree | fc9dbf4911b0ec70447a27591743adde8f6b908c /src/lib | |
parent | 14931f8ab7a337e1d1f0d6ff78c8d42ffeaf0189 (diff) | |
download | openbsd-ea13120433234de14cfef017f9b900c5d4e3874e.tar.gz openbsd-ea13120433234de14cfef017f9b900c5d4e3874e.tar.bz2 openbsd-ea13120433234de14cfef017f9b900c5d4e3874e.zip |
It's not really possible to make strtonum() deal with unsigned long
long values properly so don't bother trying. This greatly simplifies
the code. tedu@ OK with input from otto@ and others.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libc/stdlib/strtonum.3 | 19 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtonum.c | 37 |
2 files changed, 21 insertions, 35 deletions
diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3 index 7a8d7faf6d..3e3e255e55 100644 --- a/src/lib/libc/stdlib/strtonum.3 +++ b/src/lib/libc/stdlib/strtonum.3 | |||
@@ -1,4 +1,4 @@ | |||
1 | .\" $OpenBSD: strtonum.3,v 1.5 2004/05/06 06:19:01 tedu Exp $ | 1 | .\" $OpenBSD: strtonum.3,v 1.6 2004/08/03 19:38:01 millert Exp $ |
2 | .\" | 2 | .\" |
3 | .\" Copyright (c) 2004 Ted Unangst | 3 | .\" Copyright (c) 2004 Ted Unangst |
4 | .\" | 4 | .\" |
@@ -23,11 +23,11 @@ | |||
23 | .Sh SYNOPSIS | 23 | .Sh SYNOPSIS |
24 | .Fd #include <stdlib.h> | 24 | .Fd #include <stdlib.h> |
25 | .Fd #include <limits.h> | 25 | .Fd #include <limits.h> |
26 | .Ft unsigned long long | 26 | .Ft long long |
27 | .Fo strtonum | 27 | .Fo strtonum |
28 | .Fa "const char *nptr" | 28 | .Fa "const char *nptr" |
29 | .Fa "long long minval" | 29 | .Fa "long long minval" |
30 | .Fa "unsigned long long maxval" | 30 | .Fa "long long maxval" |
31 | .Fa "const char **errstr" | 31 | .Fa "const char **errstr" |
32 | .Fc | 32 | .Fc |
33 | .Sh DESCRIPTION | 33 | .Sh DESCRIPTION |
@@ -35,10 +35,9 @@ The | |||
35 | .Fn strtonum | 35 | .Fn strtonum |
36 | function converts the string in | 36 | function converts the string in |
37 | .Fa nptr | 37 | .Fa nptr |
38 | to an | 38 | to a |
39 | .Li unsigned long long | 39 | .Li long long |
40 | value. | 40 | value. |
41 | Negative values may be obtained by casting the result. | ||
42 | The | 41 | The |
43 | .Fn strtonum | 42 | .Fn strtonum |
44 | function was designed to facilitate safe, robust programming | 43 | function was designed to facilitate safe, robust programming |
@@ -57,8 +56,8 @@ or | |||
57 | .Ql - | 56 | .Ql - |
58 | sign. | 57 | sign. |
59 | .Pp | 58 | .Pp |
60 | The remainder of the string is converted to an | 59 | The remainder of the string is converted to a |
61 | .Li unsigned long long | 60 | .Li long long |
62 | value according to base 10. | 61 | value according to base 10. |
63 | .Pp | 62 | .Pp |
64 | The value obtained is then checked against the provided | 63 | The value obtained is then checked against the provided |
@@ -102,6 +101,10 @@ The above example will guarantee that the value of iterations is between | |||
102 | The given string was out of range. | 101 | The given string was out of range. |
103 | .It Bq Er EINVAL | 102 | .It Bq Er EINVAL |
104 | The given string did not consist solely of digit characters. | 103 | The given string did not consist solely of digit characters. |
104 | .It Bq Er EINVAL | ||
105 | .Ar minval | ||
106 | was larger than | ||
107 | .Ar maxval . | ||
105 | .El | 108 | .El |
106 | .Pp | 109 | .Pp |
107 | If an error occurs, errstr will be set to one of the following strings. | 110 | If an error occurs, errstr will be set to one of the following strings. |
diff --git a/src/lib/libc/stdlib/strtonum.c b/src/lib/libc/stdlib/strtonum.c index a2dfed25ca..e426388ed0 100644 --- a/src/lib/libc/stdlib/strtonum.c +++ b/src/lib/libc/stdlib/strtonum.c | |||
@@ -1,4 +1,5 @@ | |||
1 | /* $OpenBSD: strtonum.c,v 1.5 2004/07/16 18:36:05 otto Exp $ */ | 1 | /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ |
2 | |||
2 | /* | 3 | /* |
3 | * Copyright (c) 2004 Ted Unangst and Todd Miller | 4 | * Copyright (c) 2004 Ted Unangst and Todd Miller |
4 | * All rights reserved. | 5 | * All rights reserved. |
@@ -24,12 +25,11 @@ | |||
24 | #define TOOSMALL 2 | 25 | #define TOOSMALL 2 |
25 | #define TOOLARGE 3 | 26 | #define TOOLARGE 3 |
26 | 27 | ||
27 | unsigned long long | 28 | long long |
28 | strtonum(const char *numstr, long long minval, unsigned long long umaxval, | 29 | strtonum(const char *numstr, long long minval, long long maxval, |
29 | const char **errstrp) | 30 | const char **errstrp) |
30 | { | 31 | { |
31 | long long ll, maxval = (long long)umaxval; | 32 | long long ll = 0; |
32 | unsigned long long ull = 0; | ||
33 | char *ep; | 33 | char *ep; |
34 | int error = 0; | 34 | int error = 0; |
35 | struct errval { | 35 | struct errval { |
@@ -44,24 +44,9 @@ strtonum(const char *numstr, long long minval, unsigned long long umaxval, | |||
44 | 44 | ||
45 | ev[0].err = errno; | 45 | ev[0].err = errno; |
46 | errno = 0; | 46 | errno = 0; |
47 | if (umaxval > LLONG_MAX ) { | 47 | if (minval > maxval) |
48 | if (minval < 0) { | 48 | error = INVALID; |
49 | error = INVALID; | 49 | else { |
50 | goto done; | ||
51 | } | ||
52 | ull = strtoull(numstr, &ep, 10); | ||
53 | if (numstr == ep || *ep != '\0') | ||
54 | error = INVALID; | ||
55 | else if ((ull == ULLONG_MAX && errno == ERANGE) || | ||
56 | ull > umaxval) | ||
57 | error = TOOLARGE; | ||
58 | else if (ull < minval) | ||
59 | error = TOOSMALL; | ||
60 | } else { | ||
61 | if (minval > maxval || maxval < minval) { | ||
62 | error = INVALID; | ||
63 | goto done; | ||
64 | } | ||
65 | ll = strtoll(numstr, &ep, 10); | 50 | ll = strtoll(numstr, &ep, 10); |
66 | if (numstr == ep || *ep != '\0') | 51 | if (numstr == ep || *ep != '\0') |
67 | error = INVALID; | 52 | error = INVALID; |
@@ -69,14 +54,12 @@ strtonum(const char *numstr, long long minval, unsigned long long umaxval, | |||
69 | error = TOOSMALL; | 54 | error = TOOSMALL; |
70 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) | 55 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) |
71 | error = TOOLARGE; | 56 | error = TOOLARGE; |
72 | ull = (unsigned long long)ll; | ||
73 | } | 57 | } |
74 | done: | ||
75 | if (errstrp != NULL) | 58 | if (errstrp != NULL) |
76 | *errstrp = ev[error].errstr; | 59 | *errstrp = ev[error].errstr; |
77 | errno = ev[error].err; | 60 | errno = ev[error].err; |
78 | if (error) | 61 | if (error) |
79 | ull = 0; | 62 | ll = 0; |
80 | 63 | ||
81 | return (ull); | 64 | return (ll); |
82 | } | 65 | } |