Coverage Report

Created: 2026-10-17 06:17

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