Coverage Report

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