Coverage Report

Created: 2023-06-07 06:47

/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
338k
{
48
338k
    bool valid = false;
49
50
338k
    if (ep != p) {
51
  /* check for valid separator (including '\0') */
52
338k
  if (sep == NULL)
53
93.3k
      sep = "";
54
584k
  do {
55
584k
      if (*ep == *sep)
56
338k
    valid = true;
57
584k
  } while (*sep++ != '\0');
58
338k
    }
59
338k
    return valid;
60
338k
}
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
339k
{
72
339k
    const char *errstr;
73
339k
    char *ep;
74
339k
    id_t ret;
75
339k
    debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL);
76
77
339k
    ret = sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr);
78
339k
    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
339k
  if (ret == (id_t)-1 || ret == (id_t)UINT_MAX || !valid_separator(p, ep, sep)) {
84
197
      errstr = N_("invalid value");
85
197
      errno = EINVAL;
86
197
      ret = 0;
87
197
  }
88
339k
    }
89
339k
    if (errstrp != NULL)
90
339k
  *errstrp = errstr;
91
339k
    if (endp != NULL)
92
245k
  *endp = ep;
93
339k
    debug_return_id_t(ret);
94
339k
}
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
93.5k
{
107
93.5k
    return sudo_strtoidx_v1(p, NULL, NULL, errstrp);
108
93.5k
}