Coverage Report

Created: 2023-06-07 06:46

/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
5.08k
{
49
5.08k
    enum strtonum_err errval = STN_INITIAL;
50
5.08k
    long long lastval, result = 0;
51
5.08k
    const char *cp = str;
52
5.08k
    unsigned char ch;
53
5.08k
    int remainder;
54
5.08k
    char sign;
55
56
5.08k
    if (minval > maxval) {
57
0
  errval = STN_INVALID;
58
0
  goto done;
59
0
    }
60
61
    /* Trim leading space and check sign, if any. */
62
5.28k
    do {
63
5.28k
  ch = *cp++;
64
5.28k
    } while (isspace(ch));
65
5.08k
    switch (ch) {
66
585
    case '-':
67
585
  sign = '-';
68
585
  ch = *cp++;
69
585
  break;
70
195
    case '+':
71
195
  ch = *cp++;
72
195
  FALLTHROUGH;
73
4.50k
    default:
74
4.50k
  sign = '+';
75
4.50k
  break;
76
5.08k
    }
77
78
    /*
79
     * To prevent overflow we determine the highest (or lowest in
80
     * the case of negative numbers) value result can have *before*
81
     * if its multiplied (divided) by 10 as well as the remainder.
82
     * If result matches this value and the next digit is larger than
83
     * the remainder, we know the result is out of range.
84
     * The remainder is always positive since it is compared against
85
     * an unsigned digit.
86
     */
87
5.08k
    if (sign == '-') {
88
585
  lastval = minval / 10;
89
585
  remainder = -(minval % 10);
90
585
  if (remainder < 0) {
91
2
      lastval += 1;
92
2
      remainder += 10;
93
2
  }
94
1.34k
  for (;; ch = *cp++) {
95
1.34k
      if (!isdigit(ch))
96
275
    break;
97
1.06k
      ch -= '0';
98
1.06k
      if (result < lastval || (result == lastval && ch > remainder)) {
99
    /* Skip remaining digits. */
100
538
    do {
101
538
        ch = *cp++;
102
538
    } while (isdigit(ch));
103
310
    errval = STN_TOOSMALL;
104
310
    break;
105
759
      } else {
106
759
    result *= 10;
107
759
    result -= ch;
108
759
    errval = STN_VALID;
109
759
      }
110
1.06k
  }
111
585
  if (result > maxval)
112
0
      errval = STN_TOOBIG;
113
4.50k
    } else {
114
4.50k
  lastval = maxval / 10;
115
4.50k
  remainder = maxval % 10;
116
20.4k
  for (;; ch = *cp++) {
117
20.4k
      if (!isdigit(ch))
118
3.90k
    break;
119
16.5k
      ch -= '0';
120
16.5k
      if (result > lastval || (result == lastval && ch > remainder)) {
121
    /* Skip remaining digits. */
122
879
    do {
123
879
        ch = *cp++;
124
879
    } while (isdigit(ch));
125
601
    errval = STN_TOOBIG;
126
601
    break;
127
15.9k
      } else {
128
15.9k
    result *= 10;
129
15.9k
    result += ch;
130
15.9k
    errval = STN_VALID;
131
15.9k
      }
132
16.5k
  }
133
4.50k
  if (result < minval)
134
6
      errval = STN_TOOSMALL;
135
4.50k
    }
136
137
5.08k
done:
138
5.08k
    switch (errval) {
139
1.05k
    case STN_INITIAL:
140
4.16k
    case STN_VALID:
141
4.16k
  if (errstrp != NULL)
142
4.16k
      *errstrp = NULL;
143
4.16k
  break;
144
0
    case STN_INVALID:
145
0
  result = 0;
146
0
  errno = EINVAL;
147
0
  if (errstrp != NULL)
148
0
      *errstrp = N_("invalid value");
149
0
  break;
150
316
    case STN_TOOSMALL:
151
316
  result = 0;
152
316
  errno = ERANGE;
153
316
  if (errstrp != NULL)
154
316
      *errstrp = N_("value too small");
155
316
  break;
156
601
    case STN_TOOBIG:
157
601
  result = 0;
158
601
  errno = ERANGE;
159
601
  if (errstrp != NULL)
160
601
      *errstrp = N_("value too large");
161
601
  break;
162
5.08k
    }
163
5.08k
    if (endp != NULL) {
164
5.08k
  if (errval == STN_INITIAL || errval == STN_INVALID)
165
1.05k
      *endp = (char *)str;
166
4.03k
  else
167
4.03k
      *endp = (char *)(cp - 1);
168
5.08k
    }
169
5.08k
    return result;
170
5.08k
}
171
172
/*
173
 * Convert a string to a number in the range [minval, maxval]
174
 */
175
long long
176
sudo_strtonum(const char *str, long long minval, long long maxval,
177
    const char **errstrp)
178
5.08k
{
179
5.08k
    const char *errstr;
180
5.08k
    char *ep;
181
5.08k
    long long ret;
182
183
5.08k
    ret = sudo_strtonumx(str, minval, maxval, &ep, &errstr);
184
    /* Check for empty string and terminating NUL. */
185
5.08k
    if (str == ep || *ep != '\0') {
186
1.06k
  errno = EINVAL;
187
1.06k
  errstr = N_("invalid value");
188
1.06k
  ret = 0;
189
1.06k
    }
190
5.08k
    if (errstrp != NULL)
191
5.08k
  *errstrp = errstr;
192
5.08k
    return ret;
193
5.08k
}