Coverage Report

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