Coverage Report

Created: 2025-10-10 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
424
{
44
424
    enum strtonum_err errval = STN_INITIAL;
45
424
    long long lastval, result = 0;
46
424
    const char *cp = str;
47
424
    int remainder;
48
424
    char ch, sign;
49
50
424
    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
642
    do {
57
642
  ch = *cp++;
58
642
    } while (isspace((unsigned char)ch));
59
424
    switch (ch) {
60
57
    case '-':
61
57
  sign = '-';
62
57
  ch = *cp++;
63
57
  break;
64
6
    case '+':
65
6
  ch = *cp++;
66
6
  FALLTHROUGH;
67
367
    default:
68
367
  sign = '+';
69
367
  break;
70
424
    }
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
424
    if (sign == '-') {
82
57
  lastval = minval / 10;
83
57
  remainder = -(int)(minval % 10);
84
57
  if (remainder < 0) {
85
9
      lastval += 1;
86
9
      remainder += 10;
87
9
  }
88
3.58k
  for (;; ch = *cp++) {
89
3.58k
      if (!isdigit((unsigned char)ch))
90
19
    break;
91
3.56k
      ch -= '0';
92
3.56k
      if (result < lastval || (result == lastval && ch > remainder)) {
93
    /* Skip remaining digits. */
94
1.94M
    do {
95
1.94M
        ch = *cp++;
96
1.94M
    } while (isdigit((unsigned char)ch));
97
38
    errval = STN_TOOSMALL;
98
38
    break;
99
3.52k
      } else {
100
3.52k
    result *= 10;
101
3.52k
    result -= ch;
102
3.52k
    errval = STN_VALID;
103
3.52k
      }
104
3.56k
  }
105
57
  if (result > maxval)
106
0
      errval = STN_TOOBIG;
107
367
    } else {
108
367
  lastval = maxval / 10;
109
367
  remainder = (int)(maxval % 10);
110
5.60k
  for (;; ch = *cp++) {
111
5.60k
      if (!isdigit((unsigned char)ch))
112
266
    break;
113
5.34k
      ch -= '0';
114
5.34k
      if (result > lastval || (result == lastval && ch > remainder)) {
115
    /* Skip remaining digits. */
116
2.11M
    do {
117
2.11M
        ch = *cp++;
118
2.11M
    } while (isdigit((unsigned char)ch));
119
101
    errval = STN_TOOBIG;
120
101
    break;
121
5.24k
      } else {
122
5.24k
    result *= 10;
123
5.24k
    result += ch;
124
5.24k
    errval = STN_VALID;
125
5.24k
      }
126
5.34k
  }
127
367
  if (result < minval)
128
8
      errval = STN_TOOSMALL;
129
367
    }
130
131
424
done:
132
424
    switch (errval) {
133
17
    case STN_INITIAL:
134
277
    case STN_VALID:
135
277
  if (errstrp != NULL)
136
277
      *errstrp = NULL;
137
277
  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
46
    case STN_TOOSMALL:
145
46
  result = 0;
146
46
  errno = ERANGE;
147
46
  if (errstrp != NULL)
148
46
      *errstrp = N_("value too small");
149
46
  break;
150
101
    case STN_TOOBIG:
151
101
  result = 0;
152
101
  errno = ERANGE;
153
101
  if (errstrp != NULL)
154
101
      *errstrp = N_("value too large");
155
101
  break;
156
424
    }
157
424
    if (endp != NULL) {
158
424
  if (errval == STN_INITIAL || errval == STN_INVALID)
159
17
      *endp = (char *)str;
160
407
  else
161
407
      *endp = (char *)(cp - 1);
162
424
    }
163
424
    return result;
164
424
}
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
424
{
173
424
    const char *errstr;
174
424
    char *ep;
175
424
    long long ret;
176
177
424
    ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr);
178
    /* Check for empty string and terminating NUL. */
179
424
    if (str == ep || *ep != '\0') {
180
32
  errno = EINVAL;
181
32
  errstr = N_("invalid value");
182
32
  ret = 0;
183
32
    }
184
424
    if (errstrp != NULL)
185
424
  *errstrp = errstr;
186
424
    return ret;
187
424
}