Coverage Report

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