Coverage Report

Created: 2026-02-14 06:04

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