Coverage Report

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