Coverage Report

Created: 2026-02-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/lib/util/util_id.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
4
   Helper routines for uid and gid arrays
5
6
   Copyright (C) Guenther Deschner 2009
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22
#include "replace.h"
23
#include "lib/util/samba_util.h"
24
25
/****************************************************************************
26
 Add a gid to an array of gids if it's not already there.
27
****************************************************************************/
28
29
bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
30
           gid_t **gids, uint32_t *num_gids)
31
0
{
32
0
  uint32_t i;
33
34
0
  if ((*num_gids != 0) && (*gids == NULL)) {
35
    /*
36
     * A former call to this routine has failed to allocate memory
37
     */
38
0
    return false;
39
0
  }
40
41
0
  for (i=0; i<*num_gids; i++) {
42
0
    if ((*gids)[i] == gid) {
43
0
      return true;
44
0
    }
45
0
  }
46
47
0
  *gids = talloc_realloc(mem_ctx, *gids, gid_t, *num_gids+1);
48
0
  if (*gids == NULL) {
49
0
    *num_gids = 0;
50
0
    return false;
51
0
  }
52
53
0
  (*gids)[*num_gids] = gid;
54
0
  *num_gids += 1;
55
0
  return true;
56
0
}
57
58
/****************************************************************************
59
 Add a uid to an array of uids if it's not already there.
60
****************************************************************************/
61
62
bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
63
           uid_t **uids, uint32_t *num_uids)
64
0
{
65
0
  uint32_t i;
66
67
0
  if ((*num_uids != 0) && (*uids == NULL)) {
68
    /*
69
     * A former call to this routine has failed to allocate memory
70
     */
71
0
    return false;
72
0
  }
73
74
0
  for (i=0; i<*num_uids; i++) {
75
0
    if ((*uids)[i] == uid) {
76
0
      return true;
77
0
    }
78
0
  }
79
80
0
  *uids = talloc_realloc(mem_ctx, *uids, uid_t, *num_uids+1);
81
0
  if (*uids == NULL) {
82
0
    *num_uids = 0;
83
0
    return false;
84
0
  }
85
86
0
  (*uids)[*num_uids] = uid;
87
0
  *num_uids += 1;
88
  return true;
89
0
}