Coverage Report

Created: 2025-08-29 07:08

/src/sudo/lib/util/strtonum.c
Line
Count
Source (jump to first uncovered line)
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
/*
20
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22
 */
23
24
#include <config.h>
25
26
#include <ctype.h>
27
#include <errno.h>
28
29
#include <sudo_compat.h>
30
#include <sudo_gettext.h>
31
#include <sudo_util.h>
32
33
enum strtonum_err {
34
    STN_INITIAL,
35
    STN_VALID,
36
    STN_INVALID,
37
    STN_TOOSMALL,
38
    STN_TOOBIG
39
};
40
41
/*
42
 * Convert a string to a number in the range [minval, maxval]
43
 * Unlike strtonum(), this returns the first non-digit in endp (if not NULL).
44
 */
45
long long
46
sudo_strtonumx(const char *str, long long minval, long long maxval, char **endp,
47
    const char **errstrp)
48
7.99k
{
49
7.99k
    enum strtonum_err errval = STN_INITIAL;
50
7.99k
    long long lastval, result = 0;
51
7.99k
    const char *cp = str;
52
7.99k
    int remainder;
53
7.99k
    char ch, sign;
54
55
7.99k
    if (minval > maxval) {
56
0
  errval = STN_INVALID;
57
0
  goto done;
58
0
    }
59
60
    /* Trim leading space and check sign, if any. */
61
7.99k
    do {
62
7.99k
  ch = *cp++;
63
7.99k
    } while (isspace((unsigned char)ch));
64
7.99k
    switch (ch) {
65
931
    case '-':
66
931
  sign = '-';
67
931
  ch = *cp++;
68
931
  break;
69
194
    case '+':
70
194
  ch = *cp++;
71
194
  FALLTHROUGH;
72
7.06k
    default:
73
7.06k
  sign = '+';
74
7.06k
  break;
75
7.99k
    }
76
77
    /*
78
     * To prevent overflow we determine the highest (or lowest in
79
     * the case of negative numbers) value result can have *before*
80
     * if its multiplied (divided) by 10 as well as the remainder.
81
     * If result matches this value and the next digit is larger than
82
     * the remainder, we know the result is out of range.
83
     * The remainder is always positive since it is compared against
84
     * an unsigned digit.
85
     */
86
7.99k
    if (sign == '-') {
87
931
  lastval = minval / 10;
88
931
  remainder = -(int)(minval % 10);
89
931
  if (remainder < 0) {
90
0
      lastval += 1;
91
0
      remainder += 10;
92
0
  }
93
10.8k
  for (;; ch = *cp++) {
94
10.8k
      if (!isdigit((unsigned char)ch))
95
898
    break;
96
9.94k
      ch -= '0';
97
9.94k
      if (result < lastval || (result == lastval && ch > remainder)) {
98
    /* Skip remaining digits. */
99
12.1k
    do {
100
12.1k
        ch = *cp++;
101
12.1k
    } while (isdigit((unsigned char)ch));
102
33
    errval = STN_TOOSMALL;
103
33
    break;
104
9.91k
      } else {
105
9.91k
    result *= 10;
106
9.91k
    result -= ch;
107
9.91k
    errval = STN_VALID;
108
9.91k
      }
109
9.94k
  }
110
931
  if (result > maxval)
111
0
      errval = STN_TOOBIG;
112
7.06k
    } else {
113
7.06k
  lastval = maxval / 10;
114
7.06k
  remainder = (int)(maxval % 10);
115
22.8k
  for (;; ch = *cp++) {
116
22.8k
      if (!isdigit((unsigned char)ch))
117
6.98k
    break;
118
15.8k
      ch -= '0';
119
15.8k
      if (result > lastval || (result == lastval && ch > remainder)) {
120
    /* Skip remaining digits. */
121
2.75k
    do {
122
2.75k
        ch = *cp++;
123
2.75k
    } while (isdigit((unsigned char)ch));
124
72
    errval = STN_TOOBIG;
125
72
    break;
126
15.7k
      } else {
127
15.7k
    result *= 10;
128
15.7k
    result += ch;
129
15.7k
    errval = STN_VALID;
130
15.7k
      }
131
15.8k
  }
132
7.06k
  if (result < minval)
133
0
      errval = STN_TOOSMALL;
134
7.06k
    }
135
136
7.99k
done:
137
7.99k
    switch (errval) {
138
18
    case STN_INITIAL:
139
7.88k
    case STN_VALID:
140
7.88k
  if (errstrp != NULL)
141
7.88k
      *errstrp = NULL;
142
7.88k
  break;
143
0
    case STN_INVALID:
144
0
  result = 0;
145
0
  errno = EINVAL;
146
0
  if (errstrp != NULL)
147
0
      *errstrp = N_("invalid value");
148
0
  break;
149
33
    case STN_TOOSMALL:
150
33
  result = 0;
151
33
  errno = ERANGE;
152
33
  if (errstrp != NULL)
153
33
      *errstrp = N_("value too small");
154
33
  break;
155
72
    case STN_TOOBIG:
156
72
  result = 0;
157
72
  errno = ERANGE;
158
72
  if (errstrp != NULL)
159
72
      *errstrp = N_("value too large");
160
72
  break;
161
7.99k
    }
162
7.99k
    if (endp != NULL) {
163
7.99k
  if (errval == STN_INITIAL || errval == STN_INVALID)
164
18
      *endp = (char *)str;
165
7.97k
  else
166
7.97k
      *endp = (char *)(cp - 1);
167
7.99k
    }
168
7.99k
    return result;
169
7.99k
}
170
171
/*
172
 * Convert a string to a number in the range [minval, maxval]
173
 */
174
long long
175
sudo_strtonum(const char *str, long long minval, long long maxval,
176
    const char **errstrp)
177
7.99k
{
178
7.99k
    const char *errstr;
179
7.99k
    char *ep;
180
7.99k
    long long ret;
181
182
7.99k
    ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr);
183
    /* Check for empty string and terminating NUL. */
184
7.99k
    if (str == ep || *ep != '\0') {
185
54
  errno = EINVAL;
186
54
  errstr = N_("invalid value");
187
54
  ret = 0;
188
54
    }
189
7.99k
    if (errstrp != NULL)
190
7.99k
  *errstrp = errstr;
191
7.99k
    return ret;
192
7.99k
}