Coverage Report

Created: 2026-02-14 06:05

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
227k
{
44
227k
    enum strtonum_err errval = STN_INITIAL;
45
227k
    long long lastval, result = 0;
46
227k
    const char *cp = str;
47
227k
    int remainder;
48
227k
    char ch, sign;
49
50
227k
    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
228k
    do {
57
228k
  ch = *cp++;
58
228k
    } while (isspace((unsigned char)ch));
59
227k
    switch (ch) {
60
4.26k
    case '-':
61
4.26k
  sign = '-';
62
4.26k
  ch = *cp++;
63
4.26k
  break;
64
936
    case '+':
65
936
  ch = *cp++;
66
936
  FALLTHROUGH;
67
223k
    default:
68
223k
  sign = '+';
69
223k
  break;
70
227k
    }
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
227k
    if (sign == '-') {
82
4.26k
  lastval = minval / 10;
83
4.26k
  remainder = -(int)(minval % 10);
84
4.26k
  if (remainder < 0) {
85
431
      lastval += 1;
86
431
      remainder += 10;
87
431
  }
88
25.3k
  for (;; ch = *cp++) {
89
25.3k
      if (!isdigit((unsigned char)ch))
90
3.74k
    break;
91
21.6k
      ch -= '0';
92
21.6k
      if (result < lastval || (result == lastval && ch > remainder)) {
93
    /* Skip remaining digits. */
94
3.13M
    do {
95
3.13M
        ch = *cp++;
96
3.13M
    } while (isdigit((unsigned char)ch));
97
513
    errval = STN_TOOSMALL;
98
513
    break;
99
21.0k
      } else {
100
21.0k
    result *= 10;
101
21.0k
    result -= ch;
102
21.0k
    errval = STN_VALID;
103
21.0k
      }
104
21.6k
  }
105
4.26k
  if (result > maxval)
106
0
      errval = STN_TOOBIG;
107
223k
    } else {
108
223k
  lastval = maxval / 10;
109
223k
  remainder = (int)(maxval % 10);
110
627k
  for (;; ch = *cp++) {
111
627k
      if (!isdigit((unsigned char)ch))
112
221k
    break;
113
406k
      ch -= '0';
114
406k
      if (result > lastval || (result == lastval && ch > remainder)) {
115
    /* Skip remaining digits. */
116
1.07M
    do {
117
1.07M
        ch = *cp++;
118
1.07M
    } while (isdigit((unsigned char)ch));
119
1.18k
    errval = STN_TOOBIG;
120
1.18k
    break;
121
404k
      } else {
122
404k
    result *= 10;
123
404k
    result += ch;
124
404k
    errval = STN_VALID;
125
404k
      }
126
406k
  }
127
223k
  if (result < minval)
128
319
      errval = STN_TOOSMALL;
129
223k
    }
130
131
227k
done:
132
227k
    switch (errval) {
133
1.45k
    case STN_INITIAL:
134
225k
    case STN_VALID:
135
225k
  if (errstrp != NULL)
136
225k
      *errstrp = NULL;
137
225k
  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
832
    case STN_TOOSMALL:
145
832
  result = 0;
146
832
  errno = ERANGE;
147
832
  if (errstrp != NULL)
148
832
      *errstrp = N_("value too small");
149
832
  break;
150
1.18k
    case STN_TOOBIG:
151
1.18k
  result = 0;
152
1.18k
  errno = ERANGE;
153
1.18k
  if (errstrp != NULL)
154
1.18k
      *errstrp = N_("value too large");
155
1.18k
  break;
156
227k
    }
157
227k
    if (endp != NULL) {
158
227k
  if (errval == STN_INITIAL || errval == STN_INVALID)
159
1.45k
      *endp = (char *)str;
160
225k
  else
161
225k
      *endp = (char *)(cp - 1);
162
227k
    }
163
227k
    return result;
164
227k
}
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
75.2k
{
173
75.2k
    const char *errstr;
174
75.2k
    char *ep;
175
75.2k
    long long ret;
176
177
75.2k
    ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr);
178
    /* Check for empty string and terminating NUL. */
179
75.2k
    if (str == ep || *ep != '\0') {
180
1.60k
  errno = EINVAL;
181
1.60k
  errstr = N_("invalid value");
182
1.60k
  ret = 0;
183
1.60k
    }
184
75.2k
    if (errstrp != NULL)
185
73.5k
  *errstrp = errstr;
186
75.2k
    return ret;
187
75.2k
}