Coverage Report

Created: 2025-10-28 06:13

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