Coverage Report

Created: 2025-10-13 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/lib/util/strtoid.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2013-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 <sys/types.h>      /* for id_t */
22
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif
28
#include <ctype.h>
29
#include <errno.h>
30
#include <limits.h>
31
32
#include <sudo_compat.h>
33
#include <sudo_debug.h>
34
#include <sudo_gettext.h>
35
#include <sudo_util.h>
36
37
/*
38
 * Make sure that the ID ends with a valid separator char.
39
 */
40
static bool
41
valid_separator(const char *p, const char *ep, const char *sep)
42
719k
{
43
719k
    bool valid = false;
44
45
719k
    if (ep != p) {
46
  /* check for valid separator (including '\0') */
47
719k
  if (sep == NULL)
48
60.6k
      sep = "";
49
1.37M
  do {
50
1.37M
      if (*ep == *sep)
51
719k
    valid = true;
52
1.37M
  } while (*sep++ != '\0');
53
719k
    }
54
719k
    return valid;
55
719k
}
56
57
/*
58
 * Parse a uid/gid in string form.
59
 * If sep is non-NULL, it contains valid separator characters (e.g. comma, space)
60
 * If endp is non-NULL it is set to the next char after the ID.
61
 * On success, returns the parsed ID and clears errstr.
62
 * On error, returns 0 and sets errstr.
63
 */
64
id_t
65
sudo_strtoidx_v1(const char *p, const char *sep, char **endp, const char **errstrp)
66
719k
{
67
719k
    const char *errstr;
68
719k
    char *ep;
69
719k
    id_t ret;
70
719k
    debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL);
71
72
719k
    ret = (id_t)sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr);
73
719k
    if (errstr == NULL) {
74
  /*
75
   * Disallow id -1 (UINT_MAX), which means "no change"
76
   * and check for a valid separator (if specified).
77
   */
78
719k
  if (ret == (id_t)-1 || ret == (id_t)UINT_MAX || !valid_separator(p, ep, sep)) {
79
269
      errstr = N_("invalid value");
80
269
      errno = EINVAL;
81
269
      ret = 0;
82
269
  }
83
719k
    }
84
719k
    if (errstrp != NULL)
85
719k
  *errstrp = errstr;
86
719k
    if (endp != NULL)
87
658k
  *endp = ep;
88
719k
    debug_return_id_t(ret);
89
719k
}
90
91
/* Backward compatibility */
92
id_t
93
sudo_strtoid_v1(const char *p, const char *sep, char **endp, const char **errstrp)
94
0
{
95
0
    return sudo_strtoidx_v1(p, sep, endp, errstrp);
96
0
}
97
98
/* Simplified interface */
99
id_t
100
sudo_strtoid_v2(const char *p, const char **errstrp)
101
60.8k
{
102
60.8k
    return sudo_strtoidx_v1(p, NULL, NULL, errstrp);
103
60.8k
}