Coverage Report

Created: 2025-10-13 06:08

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