Coverage Report

Created: 2025-07-11 06:57

/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
985k
{
49
985k
    enum strtonum_err errval = STN_INITIAL;
50
985k
    long long lastval, result = 0;
51
985k
    const char *cp = str;
52
985k
    int remainder;
53
985k
    char ch, sign;
54
55
985k
    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
985k
    do {
62
985k
  ch = *cp++;
63
985k
    } while (isspace((unsigned char)ch));
64
985k
    switch (ch) {
65
2.61k
    case '-':
66
2.61k
  sign = '-';
67
2.61k
  ch = *cp++;
68
2.61k
  break;
69
262
    case '+':
70
262
  ch = *cp++;
71
262
  FALLTHROUGH;
72
982k
    default:
73
982k
  sign = '+';
74
982k
  break;
75
985k
    }
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
985k
    if (sign == '-') {
87
2.61k
  lastval = minval / 10;
88
2.61k
  remainder = -(int)(minval % 10);
89
2.61k
  if (remainder < 0) {
90
11
      lastval += 1;
91
11
      remainder += 10;
92
11
  }
93
13.7k
  for (;; ch = *cp++) {
94
13.7k
      if (!isdigit((unsigned char)ch))
95
2.60k
    break;
96
11.1k
      ch -= '0';
97
11.1k
      if (result < lastval || (result == lastval && ch > remainder)) {
98
    /* Skip remaining digits. */
99
207
    do {
100
207
        ch = *cp++;
101
207
    } while (isdigit((unsigned char)ch));
102
13
    errval = STN_TOOSMALL;
103
13
    break;
104
11.1k
      } else {
105
11.1k
    result *= 10;
106
11.1k
    result -= ch;
107
11.1k
    errval = STN_VALID;
108
11.1k
      }
109
11.1k
  }
110
2.61k
  if (result > maxval)
111
0
      errval = STN_TOOBIG;
112
982k
    } else {
113
982k
  lastval = maxval / 10;
114
982k
  remainder = (int)(maxval % 10);
115
2.60M
  for (;; ch = *cp++) {
116
2.60M
      if (!isdigit((unsigned char)ch))
117
982k
    break;
118
1.62M
      ch -= '0';
119
1.62M
      if (result > lastval || (result == lastval && ch > remainder)) {
120
    /* Skip remaining digits. */
121
244
    do {
122
244
        ch = *cp++;
123
244
    } while (isdigit((unsigned char)ch));
124
45
    errval = STN_TOOBIG;
125
45
    break;
126
1.62M
      } else {
127
1.62M
    result *= 10;
128
1.62M
    result += ch;
129
1.62M
    errval = STN_VALID;
130
1.62M
      }
131
1.62M
  }
132
982k
  if (result < minval)
133
26
      errval = STN_TOOSMALL;
134
982k
    }
135
136
985k
done:
137
985k
    switch (errval) {
138
179
    case STN_INITIAL:
139
985k
    case STN_VALID:
140
985k
  if (errstrp != NULL)
141
985k
      *errstrp = NULL;
142
985k
  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
39
    case STN_TOOSMALL:
150
39
  result = 0;
151
39
  errno = ERANGE;
152
39
  if (errstrp != NULL)
153
39
      *errstrp = N_("value too small");
154
39
  break;
155
45
    case STN_TOOBIG:
156
45
  result = 0;
157
45
  errno = ERANGE;
158
45
  if (errstrp != NULL)
159
45
      *errstrp = N_("value too large");
160
45
  break;
161
985k
    }
162
985k
    if (endp != NULL) {
163
985k
  if (errval == STN_INITIAL || errval == STN_INVALID)
164
179
      *endp = (char *)str;
165
985k
  else
166
985k
      *endp = (char *)(cp - 1);
167
985k
    }
168
985k
    return result;
169
985k
}
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
57.5k
{
178
57.5k
    const char *errstr;
179
57.5k
    char *ep;
180
57.5k
    long long ret;
181
182
57.5k
    ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr);
183
    /* Check for empty string and terminating NUL. */
184
57.5k
    if (str == ep || *ep != '\0') {
185
35
  errno = EINVAL;
186
35
  errstr = N_("invalid value");
187
35
  ret = 0;
188
35
    }
189
57.5k
    if (errstrp != NULL)
190
57.5k
  *errstrp = errstr;
191
57.5k
    return ret;
192
57.5k
}