Coverage Report

Created: 2025-10-10 07: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
0
{
43
0
    bool valid = false;
44
45
0
    if (ep != p) {
46
  /* check for valid separator (including '\0') */
47
0
  if (sep == NULL)
48
0
      sep = "";
49
0
  do {
50
0
      if (*ep == *sep)
51
0
    valid = true;
52
0
  } while (*sep++ != '\0');
53
0
    }
54
0
    return valid;
55
0
}
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
0
{
67
0
    const char *errstr;
68
0
    char *ep;
69
0
    id_t ret;
70
0
    debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL);
71
72
0
    ret = (id_t)sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr);
73
0
    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
0
  if (ret == (id_t)-1 || ret == (id_t)UINT_MAX || !valid_separator(p, ep, sep)) {
79
0
      errstr = N_("invalid value");
80
0
      errno = EINVAL;
81
0
      ret = 0;
82
0
  }
83
0
    }
84
0
    if (errstrp != NULL)
85
0
  *errstrp = errstr;
86
0
    if (endp != NULL)
87
0
  *endp = ep;
88
0
    debug_return_id_t(ret);
89
0
}
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
0
{
102
0
    return sudo_strtoidx_v1(p, NULL, NULL, errstrp);
103
0
}