Coverage Report

Created: 2026-01-10 06:48

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