Coverage Report

Created: 2023-06-07 06:47

/src/sudo/lib/util/gidlist.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2013-2015 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 <stdlib.h>
27
#include <grp.h>
28
29
#include "sudo_compat.h"
30
#include "sudo_debug.h"
31
#include "sudo_fatal.h"
32
#include "sudo_gettext.h"
33
#include "sudo_util.h"
34
35
/*
36
 * Parse a comma-separated list of gids into an allocated array of GETGROUPS_T.
37
 * If a pointer to the base gid is specified, it is stored as the first element
38
 * in the array.
39
 * Returns the number of gids in the allocated array.
40
 */
41
int
42
sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp)
43
351
{
44
351
    int ngids = 0;
45
351
    GETGROUPS_T *gids;
46
351
    const char *cp = gidstr;
47
351
    const char *errstr;
48
351
    char *ep;
49
351
    debug_decl(sudo_parse_gids, SUDO_DEBUG_UTIL);
50
51
    /* Count groups. */
52
351
    if (*cp != '\0') {
53
351
  ngids++;
54
835k
  do {
55
835k
      if (*cp++ == ',')
56
278k
    ngids++;
57
835k
  } while (*cp != '\0');
58
351
    }
59
    /* Base gid is optional. */
60
351
    if (basegid != NULL)
61
351
  ngids++;
62
    /* Allocate and fill in array. */
63
351
    if (ngids != 0) {
64
351
  gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
65
351
  if (gids == NULL) {
66
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
67
0
      debug_return_int(-1);
68
0
  }
69
351
  ngids = 0;
70
351
  if (basegid != NULL)
71
351
      gids[ngids++] = *basegid;
72
351
  cp = gidstr;
73
245k
  do {
74
245k
      gids[ngids] = (GETGROUPS_T) sudo_strtoidx(cp, ",", &ep, &errstr);
75
245k
      if (errstr != NULL) {
76
23
    sudo_warnx(U_("%s: %s"), cp, U_(errstr));
77
23
    free(gids);
78
23
    debug_return_int(-1);
79
23
      }
80
245k
      if (basegid == NULL || gids[ngids] != *basegid)
81
245k
    ngids++;
82
245k
      cp = ep + 1;
83
245k
  } while (*ep != '\0');
84
328
  *gidsp = gids;
85
328
    }
86
328
    debug_return_int(ngids);
87
328
}