Coverage Report

Created: 2025-07-23 06:41

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