Coverage Report

Created: 2025-07-11 06:58

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