/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 | 104 | { |
49 | 104 | enum strtonum_err errval = STN_INITIAL; |
50 | 104 | long long lastval, result = 0; |
51 | 104 | const char *cp = str; |
52 | 104 | unsigned char ch; |
53 | 104 | int remainder; |
54 | 104 | char sign; |
55 | | |
56 | 104 | if (minval > maxval) { |
57 | 0 | errval = STN_INVALID; |
58 | 0 | goto done; |
59 | 0 | } |
60 | | |
61 | | /* Trim leading space and check sign, if any. */ |
62 | 104 | do { |
63 | 104 | ch = *cp++; |
64 | 104 | } while (isspace(ch)); |
65 | 104 | switch (ch) { |
66 | 0 | case '-': |
67 | 0 | sign = '-'; |
68 | 0 | ch = *cp++; |
69 | 0 | break; |
70 | 0 | case '+': |
71 | 0 | ch = *cp++; |
72 | 0 | FALLTHROUGH; |
73 | 104 | default: |
74 | 104 | sign = '+'; |
75 | 104 | break; |
76 | 104 | } |
77 | | |
78 | | /* |
79 | | * To prevent overflow we determine the highest (or lowest in |
80 | | * the case of negative numbers) value result can have *before* |
81 | | * if its multiplied (divided) by 10 as well as the remainder. |
82 | | * If result matches this value and the next digit is larger than |
83 | | * the remainder, we know the result is out of range. |
84 | | * The remainder is always positive since it is compared against |
85 | | * an unsigned digit. |
86 | | */ |
87 | 104 | if (sign == '-') { |
88 | 0 | lastval = minval / 10; |
89 | 0 | remainder = -(minval % 10); |
90 | 0 | if (remainder < 0) { |
91 | 0 | lastval += 1; |
92 | 0 | remainder += 10; |
93 | 0 | } |
94 | 0 | for (;; ch = *cp++) { |
95 | 0 | if (!isdigit(ch)) |
96 | 0 | break; |
97 | 0 | ch -= '0'; |
98 | 0 | if (result < lastval || (result == lastval && ch > remainder)) { |
99 | | /* Skip remaining digits. */ |
100 | 0 | do { |
101 | 0 | ch = *cp++; |
102 | 0 | } while (isdigit(ch)); |
103 | 0 | errval = STN_TOOSMALL; |
104 | 0 | break; |
105 | 0 | } else { |
106 | 0 | result *= 10; |
107 | 0 | result -= ch; |
108 | 0 | errval = STN_VALID; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | if (result > maxval) |
112 | 0 | errval = STN_TOOBIG; |
113 | 104 | } else { |
114 | 104 | lastval = maxval / 10; |
115 | 104 | remainder = maxval % 10; |
116 | 244 | for (;; ch = *cp++) { |
117 | 244 | if (!isdigit(ch)) |
118 | 104 | break; |
119 | 140 | ch -= '0'; |
120 | 140 | if (result > lastval || (result == lastval && ch > remainder)) { |
121 | | /* Skip remaining digits. */ |
122 | 0 | do { |
123 | 0 | ch = *cp++; |
124 | 0 | } while (isdigit(ch)); |
125 | 0 | errval = STN_TOOBIG; |
126 | 0 | break; |
127 | 140 | } else { |
128 | 140 | result *= 10; |
129 | 140 | result += ch; |
130 | 140 | errval = STN_VALID; |
131 | 140 | } |
132 | 140 | } |
133 | 104 | if (result < minval) |
134 | 0 | errval = STN_TOOSMALL; |
135 | 104 | } |
136 | | |
137 | 104 | done: |
138 | 104 | switch (errval) { |
139 | 0 | case STN_INITIAL: |
140 | 104 | case STN_VALID: |
141 | 104 | if (errstrp != NULL) |
142 | 104 | *errstrp = NULL; |
143 | 104 | break; |
144 | 0 | case STN_INVALID: |
145 | 0 | result = 0; |
146 | 0 | errno = EINVAL; |
147 | 0 | if (errstrp != NULL) |
148 | 0 | *errstrp = N_("invalid value"); |
149 | 0 | break; |
150 | 0 | case STN_TOOSMALL: |
151 | 0 | result = 0; |
152 | 0 | errno = ERANGE; |
153 | 0 | if (errstrp != NULL) |
154 | 0 | *errstrp = N_("value too small"); |
155 | 0 | break; |
156 | 0 | case STN_TOOBIG: |
157 | 0 | result = 0; |
158 | 0 | errno = ERANGE; |
159 | 0 | if (errstrp != NULL) |
160 | 0 | *errstrp = N_("value too large"); |
161 | 0 | break; |
162 | 104 | } |
163 | 104 | if (endp != NULL) { |
164 | 104 | if (errval == STN_INITIAL || errval == STN_INVALID) |
165 | 0 | *endp = (char *)str; |
166 | 104 | else |
167 | 104 | *endp = (char *)(cp - 1); |
168 | 104 | } |
169 | 104 | return result; |
170 | 104 | } |
171 | | |
172 | | /* |
173 | | * Convert a string to a number in the range [minval, maxval] |
174 | | */ |
175 | | long long |
176 | | sudo_strtonum(const char *str, long long minval, long long maxval, |
177 | | const char **errstrp) |
178 | 0 | { |
179 | 0 | const char *errstr; |
180 | 0 | char *ep; |
181 | 0 | long long ret; |
182 | |
|
183 | 0 | ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr); |
184 | | /* Check for empty string and terminating NUL. */ |
185 | 0 | if (str == ep || *ep != '\0') { |
186 | 0 | errno = EINVAL; |
187 | 0 | errstr = N_("invalid value"); |
188 | 0 | ret = 0; |
189 | 0 | } |
190 | 0 | if (errstrp != NULL) |
191 | 0 | *errstrp = errstr; |
192 | 0 | return ret; |
193 | 0 | } |