/src/sudo/lib/util/strtonum.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2013-2015, 2019-2020 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | #include <ctype.h> |
22 | | #include <errno.h> |
23 | | |
24 | | #include <sudo_compat.h> |
25 | | #include <sudo_gettext.h> |
26 | | #include <sudo_util.h> |
27 | | |
28 | | enum strtonum_err { |
29 | | STN_INITIAL, |
30 | | STN_VALID, |
31 | | STN_INVALID, |
32 | | STN_TOOSMALL, |
33 | | STN_TOOBIG |
34 | | }; |
35 | | |
36 | | /* |
37 | | * Convert a string to a number in the range [minval, maxval] |
38 | | * Unlike strtonum(), this returns the first non-digit in endp (if not NULL). |
39 | | */ |
40 | | long long |
41 | | sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp, |
42 | | const char **errstrp) |
43 | 707k | { |
44 | 707k | enum strtonum_err errval = STN_INITIAL; |
45 | 707k | long long lastval, result = 0; |
46 | 707k | const char *cp = str; |
47 | 707k | int remainder; |
48 | 707k | char ch, sign; |
49 | | |
50 | 707k | if (minval > maxval) { |
51 | 0 | errval = STN_INVALID; |
52 | 0 | goto done; |
53 | 0 | } |
54 | | |
55 | | /* Trim leading space and check sign, if any. */ |
56 | 707k | do { |
57 | 707k | ch = *cp++; |
58 | 707k | } while (isspace((unsigned char)ch)); |
59 | 707k | switch (ch) { |
60 | 3.96k | case '-': |
61 | 3.96k | sign = '-'; |
62 | 3.96k | ch = *cp++; |
63 | 3.96k | break; |
64 | 270 | case '+': |
65 | 270 | ch = *cp++; |
66 | 270 | FALLTHROUGH; |
67 | 703k | default: |
68 | 703k | sign = '+'; |
69 | 703k | break; |
70 | 707k | } |
71 | | |
72 | | /* |
73 | | * To prevent overflow we determine the highest (or lowest in |
74 | | * the case of negative numbers) value result can have *before* |
75 | | * if its multiplied (divided) by 10 as well as the remainder. |
76 | | * If result matches this value and the next digit is larger than |
77 | | * the remainder, we know the result is out of range. |
78 | | * The remainder is always positive since it is compared against |
79 | | * an unsigned digit. |
80 | | */ |
81 | 707k | if (sign == '-') { |
82 | 3.96k | lastval = minval / 10; |
83 | 3.96k | remainder = -(int)(minval % 10); |
84 | 3.96k | if (remainder < 0) { |
85 | 12 | lastval += 1; |
86 | 12 | remainder += 10; |
87 | 12 | } |
88 | 20.1k | for (;; ch = *cp++) { |
89 | 20.1k | if (!isdigit((unsigned char)ch)) |
90 | 3.95k | break; |
91 | 16.1k | ch -= '0'; |
92 | 16.1k | if (result < lastval || (result == lastval && ch > remainder)) { |
93 | | /* Skip remaining digits. */ |
94 | 206 | do { |
95 | 206 | ch = *cp++; |
96 | 206 | } while (isdigit((unsigned char)ch)); |
97 | 12 | errval = STN_TOOSMALL; |
98 | 12 | break; |
99 | 16.1k | } else { |
100 | 16.1k | result *= 10; |
101 | 16.1k | result -= ch; |
102 | 16.1k | errval = STN_VALID; |
103 | 16.1k | } |
104 | 16.1k | } |
105 | 3.96k | if (result > maxval) |
106 | 0 | errval = STN_TOOBIG; |
107 | 703k | } else { |
108 | 703k | lastval = maxval / 10; |
109 | 703k | remainder = (int)(maxval % 10); |
110 | 1.87M | for (;; ch = *cp++) { |
111 | 1.87M | if (!isdigit((unsigned char)ch)) |
112 | 703k | break; |
113 | 1.17M | ch -= '0'; |
114 | 1.17M | if (result > lastval || (result == lastval && ch > remainder)) { |
115 | | /* Skip remaining digits. */ |
116 | 307 | do { |
117 | 307 | ch = *cp++; |
118 | 307 | } while (isdigit((unsigned char)ch)); |
119 | 47 | errval = STN_TOOBIG; |
120 | 47 | break; |
121 | 1.17M | } else { |
122 | 1.17M | result *= 10; |
123 | 1.17M | result += ch; |
124 | 1.17M | errval = STN_VALID; |
125 | 1.17M | } |
126 | 1.17M | } |
127 | 703k | if (result < minval) |
128 | 28 | errval = STN_TOOSMALL; |
129 | 703k | } |
130 | | |
131 | 707k | done: |
132 | 707k | switch (errval) { |
133 | 193 | case STN_INITIAL: |
134 | 707k | case STN_VALID: |
135 | 707k | if (errstrp != NULL) |
136 | 707k | *errstrp = NULL; |
137 | 707k | break; |
138 | 0 | case STN_INVALID: |
139 | 0 | result = 0; |
140 | 0 | errno = EINVAL; |
141 | 0 | if (errstrp != NULL) |
142 | 0 | *errstrp = N_("invalid value"); |
143 | 0 | break; |
144 | 40 | case STN_TOOSMALL: |
145 | 40 | result = 0; |
146 | 40 | errno = ERANGE; |
147 | 40 | if (errstrp != NULL) |
148 | 40 | *errstrp = N_("value too small"); |
149 | 40 | break; |
150 | 47 | case STN_TOOBIG: |
151 | 47 | result = 0; |
152 | 47 | errno = ERANGE; |
153 | 47 | if (errstrp != NULL) |
154 | 47 | *errstrp = N_("value too large"); |
155 | 47 | break; |
156 | 707k | } |
157 | 707k | if (endp != NULL) { |
158 | 707k | if (errval == STN_INITIAL || errval == STN_INVALID) |
159 | 193 | *endp = (char *)str; |
160 | 707k | else |
161 | 707k | *endp = (char *)(cp - 1); |
162 | 707k | } |
163 | 707k | return result; |
164 | 707k | } |
165 | | |
166 | | /* |
167 | | * Convert a string to a number in the range [minval, maxval] |
168 | | */ |
169 | | long long |
170 | | sudo_strtonum(const char *str, long long minval, long long maxval, |
171 | | const char **errstrp) |
172 | 55.1k | { |
173 | 55.1k | const char *errstr; |
174 | 55.1k | char *ep; |
175 | 55.1k | long long ret; |
176 | | |
177 | 55.1k | ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr); |
178 | | /* Check for empty string and terminating NUL. */ |
179 | 55.1k | if (str == ep || *ep != '\0') { |
180 | 36 | errno = EINVAL; |
181 | 36 | errstr = N_("invalid value"); |
182 | 36 | ret = 0; |
183 | 36 | } |
184 | 55.1k | if (errstrp != NULL) |
185 | 55.1k | *errstrp = errstr; |
186 | 55.1k | return ret; |
187 | 55.1k | } |