/src/sudo/lib/util/strtonum.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <ctype.h> |
27 | | #include <errno.h> |
28 | | |
29 | | #include <sudo_compat.h> |
30 | | #include <sudo_gettext.h> |
31 | | #include <sudo_util.h> |
32 | | |
33 | | enum strtonum_err { |
34 | | STN_INITIAL, |
35 | | STN_VALID, |
36 | | STN_INVALID, |
37 | | STN_TOOSMALL, |
38 | | STN_TOOBIG |
39 | | }; |
40 | | |
41 | | /* |
42 | | * Convert a string to a number in the range [minval, maxval] |
43 | | * Unlike strtonum(), this returns the first non-digit in endp (if not NULL). |
44 | | */ |
45 | | long long |
46 | | sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp, |
47 | | const char **errstrp) |
48 | 426 | { |
49 | 426 | enum strtonum_err errval = STN_INITIAL; |
50 | 426 | long long lastval, result = 0; |
51 | 426 | const char *cp = str; |
52 | 426 | int remainder; |
53 | 426 | char ch, sign; |
54 | | |
55 | 426 | if (minval > maxval) { |
56 | 0 | errval = STN_INVALID; |
57 | 0 | goto done; |
58 | 0 | } |
59 | | |
60 | | /* Trim leading space and check sign, if any. */ |
61 | 638 | do { |
62 | 638 | ch = *cp++; |
63 | 638 | } while (isspace((unsigned char)ch)); |
64 | 426 | switch (ch) { |
65 | 62 | case '-': |
66 | 62 | sign = '-'; |
67 | 62 | ch = *cp++; |
68 | 62 | break; |
69 | 8 | case '+': |
70 | 8 | ch = *cp++; |
71 | 8 | FALLTHROUGH; |
72 | 364 | default: |
73 | 364 | sign = '+'; |
74 | 364 | break; |
75 | 426 | } |
76 | | |
77 | | /* |
78 | | * To prevent overflow we determine the highest (or lowest in |
79 | | * the case of negative numbers) value result can have *before* |
80 | | * if its multiplied (divided) by 10 as well as the remainder. |
81 | | * If result matches this value and the next digit is larger than |
82 | | * the remainder, we know the result is out of range. |
83 | | * The remainder is always positive since it is compared against |
84 | | * an unsigned digit. |
85 | | */ |
86 | 426 | if (sign == '-') { |
87 | 62 | lastval = minval / 10; |
88 | 62 | remainder = -(int)(minval % 10); |
89 | 62 | if (remainder < 0) { |
90 | 9 | lastval += 1; |
91 | 9 | remainder += 10; |
92 | 9 | } |
93 | 8.86k | for (;; ch = *cp++) { |
94 | 8.86k | if (!isdigit((unsigned char)ch)) |
95 | 20 | break; |
96 | 8.84k | ch -= '0'; |
97 | 8.84k | if (result < lastval || (result == lastval && ch > remainder)) { |
98 | | /* Skip remaining digits. */ |
99 | 2.07M | do { |
100 | 2.07M | ch = *cp++; |
101 | 2.07M | } while (isdigit((unsigned char)ch)); |
102 | 42 | errval = STN_TOOSMALL; |
103 | 42 | break; |
104 | 8.80k | } else { |
105 | 8.80k | result *= 10; |
106 | 8.80k | result -= ch; |
107 | 8.80k | errval = STN_VALID; |
108 | 8.80k | } |
109 | 8.84k | } |
110 | 62 | if (result > maxval) |
111 | 0 | errval = STN_TOOBIG; |
112 | 364 | } else { |
113 | 364 | lastval = maxval / 10; |
114 | 364 | remainder = (int)(maxval % 10); |
115 | 8.17k | for (;; ch = *cp++) { |
116 | 8.17k | if (!isdigit((unsigned char)ch)) |
117 | 268 | break; |
118 | 7.90k | ch -= '0'; |
119 | 7.90k | if (result > lastval || (result == lastval && ch > remainder)) { |
120 | | /* Skip remaining digits. */ |
121 | 2.10M | do { |
122 | 2.10M | ch = *cp++; |
123 | 2.10M | } while (isdigit((unsigned char)ch)); |
124 | 96 | errval = STN_TOOBIG; |
125 | 96 | break; |
126 | 7.80k | } else { |
127 | 7.80k | result *= 10; |
128 | 7.80k | result += ch; |
129 | 7.80k | errval = STN_VALID; |
130 | 7.80k | } |
131 | 7.90k | } |
132 | 364 | if (result < minval) |
133 | 10 | errval = STN_TOOSMALL; |
134 | 364 | } |
135 | | |
136 | 426 | done: |
137 | 426 | switch (errval) { |
138 | 17 | case STN_INITIAL: |
139 | 278 | case STN_VALID: |
140 | 278 | if (errstrp != NULL) |
141 | 278 | *errstrp = NULL; |
142 | 278 | break; |
143 | 0 | case STN_INVALID: |
144 | 0 | result = 0; |
145 | 0 | errno = EINVAL; |
146 | 0 | if (errstrp != NULL) |
147 | 0 | *errstrp = N_("invalid value"); |
148 | 0 | break; |
149 | 52 | case STN_TOOSMALL: |
150 | 52 | result = 0; |
151 | 52 | errno = ERANGE; |
152 | 52 | if (errstrp != NULL) |
153 | 52 | *errstrp = N_("value too small"); |
154 | 52 | break; |
155 | 96 | case STN_TOOBIG: |
156 | 96 | result = 0; |
157 | 96 | errno = ERANGE; |
158 | 96 | if (errstrp != NULL) |
159 | 96 | *errstrp = N_("value too large"); |
160 | 96 | break; |
161 | 426 | } |
162 | 426 | if (endp != NULL) { |
163 | 426 | if (errval == STN_INITIAL || errval == STN_INVALID) |
164 | 17 | *endp = (char *)str; |
165 | 409 | else |
166 | 409 | *endp = (char *)(cp - 1); |
167 | 426 | } |
168 | 426 | return result; |
169 | 426 | } |
170 | | |
171 | | /* |
172 | | * Convert a string to a number in the range [minval, maxval] |
173 | | */ |
174 | | long long |
175 | | sudo_strtonum(const char *str, long long minval, long long maxval, |
176 | | const char **errstrp) |
177 | 426 | { |
178 | 426 | const char *errstr; |
179 | 426 | char *ep; |
180 | 426 | long long ret; |
181 | | |
182 | 426 | ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr); |
183 | | /* Check for empty string and terminating NUL. */ |
184 | 426 | if (str == ep || *ep != '\0') { |
185 | 31 | errno = EINVAL; |
186 | 31 | errstr = N_("invalid value"); |
187 | 31 | ret = 0; |
188 | 31 | } |
189 | 426 | if (errstrp != NULL) |
190 | 426 | *errstrp = errstr; |
191 | 426 | return ret; |
192 | 426 | } |