Coverage Report

Created: 2026-01-09 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/redblack.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2004-2005, 2007, 2009-2015
5
 *  Todd C. Miller <Todd.Miller@sudo.ws>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
/*
21
 * Adapted from the following code written by Emin Martinian:
22
 * http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
23
 *
24
 * Copyright (c) 2001 Emin Martinian
25
 *
26
 * Redistribution and use in source and binary forms, with or without
27
 * modification, are permitted provided that neither the name of Emin
28
 * Martinian nor the names of any contributors are be used to endorse or
29
 * promote products derived from this software without specific prior
30
 * written permission.
31
 *
32
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
 */
44
45
#include <config.h>
46
47
#include <stdio.h>
48
#include <stdlib.h>
49
50
#include <sudoers.h>
51
#include <redblack.h>
52
53
static void rbrepair(struct rbtree *, struct rbnode *);
54
static void rotate_left(struct rbtree *, struct rbnode *);
55
static void rotate_right(struct rbtree *, struct rbnode *);
56
static void rbdestroy_int(struct rbtree *, struct rbnode *, void (*)(void *));
57
58
/*
59
 * Red-Black tree, see http://en.wikipedia.org/wiki/Red-black_tree
60
 *
61
 * A red-black tree is a binary search tree where each node has a color
62
 * attribute, the value of which is either red or black.  Essentially, it
63
 * is just a convenient way to express a 2-3-4 binary search tree where
64
 * the color indicates whether the node is part of a 3-node or a 4-node.
65
 * In addition to the ordinary requirements imposed on binary search
66
 * trees, we make the following additional requirements of any valid
67
 * red-black tree:
68
 *  1) Every node is either red or black.
69
 *  2) The root is black.
70
 *  3) All leaves are black.
71
 *  4) Both children of each red node are black.
72
 *  5) The paths from each leaf up to the root each contain the same
73
 *     number of black nodes.
74
 */
75
76
/*
77
 * Create a red black tree struct using the specified compare routine.
78
 * Allocates and returns the initialized (empty) tree or NULL if
79
 * memory cannot be allocated.
80
 */
81
struct rbtree *
82
rbcreate(int (*compar)(const void *, const void*))
83
57.5k
{
84
57.5k
    struct rbtree *tree;
85
57.5k
    debug_decl(rbcreate, SUDOERS_DEBUG_RBTREE);
86
87
57.5k
    if ((tree = malloc(sizeof(*tree))) == NULL) {
88
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
89
0
      "unable to allocate memory");
90
0
  debug_return_ptr(NULL);
91
0
    }
92
93
57.5k
    tree->compar = compar;
94
95
    /*
96
     * We use a self-referencing sentinel node called nil to simplify the
97
     * code by avoiding the need to check for NULL pointers.
98
     */
99
57.5k
    tree->nil.left = tree->nil.right = tree->nil.parent = &tree->nil;
100
57.5k
    tree->nil.color = black;
101
57.5k
    tree->nil.data = NULL;
102
103
    /*
104
     * Similarly, the fake root node keeps us from having to worry
105
     * about splitting the root.
106
     */
107
57.5k
    tree->root.left = tree->root.right = tree->root.parent = &tree->nil; // -V778
108
57.5k
    tree->root.color = black;
109
57.5k
    tree->root.data = NULL;
110
111
57.5k
    debug_return_ptr(tree);
112
57.5k
}
113
114
/*
115
 * Perform a left rotation starting at node.
116
 */
117
static void
118
rotate_left(struct rbtree *tree, struct rbnode *node)
119
13.3k
{
120
13.3k
    struct rbnode *child;
121
13.3k
    debug_decl(rotate_left, SUDOERS_DEBUG_RBTREE);
122
123
13.3k
    child = node->right;
124
13.3k
    node->right = child->left;
125
126
13.3k
    if (child->left != rbnil(tree))
127
3.73k
        child->left->parent = node;
128
13.3k
    child->parent = node->parent;
129
130
13.3k
    if (node == node->parent->left)
131
9.14k
  node->parent->left = child;
132
4.19k
    else
133
4.19k
  node->parent->right = child;
134
13.3k
    child->left = node;
135
13.3k
    node->parent = child;
136
137
13.3k
    debug_return;
138
13.3k
}
139
140
/*
141
 * Perform a right rotation starting at node.
142
 */
143
static void
144
rotate_right(struct rbtree *tree, struct rbnode *node)
145
12.2k
{
146
12.2k
    struct rbnode *child;
147
12.2k
    debug_decl(rotate_right, SUDOERS_DEBUG_RBTREE);
148
149
12.2k
    child = node->left;
150
12.2k
    node->left = child->right;
151
152
12.2k
    if (child->right != rbnil(tree))
153
3.29k
        child->right->parent = node;
154
12.2k
    child->parent = node->parent;
155
156
12.2k
    if (node == node->parent->left)
157
4.13k
  node->parent->left = child;
158
8.12k
    else
159
8.12k
  node->parent->right = child;
160
12.2k
    child->right = node;
161
12.2k
    node->parent = child;
162
163
12.2k
    debug_return;
164
12.2k
}
165
166
/*
167
 * Insert data pointer into a redblack tree.
168
 * Returns a 0 on success, 1 if a node matching "data" already exists
169
 * (filling in "existing" if not NULL), or -1 on malloc() failure.
170
 */
171
int
172
rbinsert(struct rbtree *tree, void *data, struct rbnode **existing)
173
386k
{
174
386k
    struct rbnode *node = rbfirst(tree);
175
386k
    struct rbnode *parent = rbroot(tree);
176
386k
    int res;
177
386k
    debug_decl(rbinsert, SUDOERS_DEBUG_RBTREE);
178
179
    /* Find correct insertion point. */
180
1.21M
    while (node != rbnil(tree)) {
181
1.10M
  parent = node;
182
1.10M
  if ((res = tree->compar(data, node->data)) == 0) {
183
269k
      if (existing != NULL)
184
269k
    *existing = node;
185
269k
      debug_return_int(1);
186
269k
  }
187
832k
  node = res < 0 ? node->left : node->right;
188
832k
    }
189
190
117k
    node = malloc(sizeof(*node));
191
117k
    if (node == NULL) {
192
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
193
0
      "unable to allocate memory");
194
0
  debug_return_int(-1);
195
0
    }
196
117k
    node->data = data;
197
117k
    node->left = node->right = rbnil(tree);
198
117k
    node->parent = parent;
199
117k
    if (parent == rbroot(tree) || tree->compar(data, parent->data) < 0)
200
73.9k
  parent->left = node;
201
43.5k
    else
202
43.5k
  parent->right = node;
203
117k
    node->color = red;
204
205
    /*
206
     * If the parent node is black we are all set, if it is red we have
207
     * the following possible cases to deal with.  We iterate through
208
     * the rest of the tree to make sure none of the required properties
209
     * is violated.
210
     *
211
     *  1) The uncle is red.  We repaint both the parent and uncle black
212
     *     and repaint the grandparent node red.
213
     *
214
     *  2) The uncle is black and the new node is the right child of its
215
     *     parent, and the parent in turn is the left child of its parent.
216
     *     We do a left rotation to switch the roles of the parent and
217
     *     child, relying on further iterations to fixup the old parent.
218
     *
219
     *  3) The uncle is black and the new node is the left child of its
220
     *     parent, and the parent in turn is the left child of its parent.
221
     *     We switch the colors of the parent and grandparent and perform
222
     *     a right rotation around the grandparent.  This makes the former
223
     *     parent the parent of the new node and the former grandparent.
224
     *
225
     * Note that because we use a sentinel for the root node we never
226
     * need to worry about replacing the root.
227
     */
228
152k
    while (node->parent->color == red) {
229
35.0k
  struct rbnode *uncle;
230
35.0k
  if (node->parent == node->parent->parent->left) {
231
16.3k
      uncle = node->parent->parent->right;
232
16.3k
      if (uncle->color == red) {
233
8.24k
    node->parent->color = black;
234
8.24k
    uncle->color = black;
235
8.24k
    node->parent->parent->color = red;
236
8.24k
    node = node->parent->parent;
237
8.24k
      } else /* if (uncle->color == black) */ {
238
8.07k
    if (node == node->parent->right) {
239
4.62k
        node = node->parent;
240
4.62k
        rotate_left(tree, node);
241
4.62k
    }
242
8.07k
    node->parent->color = black;
243
8.07k
    node->parent->parent->color = red;
244
8.07k
    rotate_right(tree, node->parent->parent);
245
8.07k
      }
246
18.6k
  } else { /* if (node->parent == node->parent->parent->right) */
247
18.6k
      uncle = node->parent->parent->left;
248
18.6k
      if (uncle->color == red) {
249
9.98k
    node->parent->color = black;
250
9.98k
    uncle->color = black;
251
9.98k
    node->parent->parent->color = red;
252
9.98k
    node = node->parent->parent;
253
9.98k
      } else /* if (uncle->color == black) */ {
254
8.70k
    if (node == node->parent->left) {
255
4.18k
        node = node->parent;
256
4.18k
        rotate_right(tree, node);
257
4.18k
    }
258
8.70k
    node->parent->color = black;
259
8.70k
    node->parent->parent->color = red;
260
8.70k
    rotate_left(tree, node->parent->parent);
261
8.70k
      }
262
18.6k
  }
263
35.0k
    }
264
117k
    rbfirst(tree)->color = black;  /* first node is always black */
265
117k
    debug_return_int(0);
266
117k
}
267
268
/*
269
 * Look for a node matching key in tree.
270
 * Returns a pointer to the node if found, else NULL.
271
 */
272
struct rbnode *
273
rbfind(struct rbtree *tree, void *key)
274
125k
{
275
125k
    struct rbnode *node = rbfirst(tree);
276
125k
    int res;
277
125k
    debug_decl(rbfind, SUDOERS_DEBUG_RBTREE);
278
279
192k
    while (node != rbnil(tree)) {
280
126k
  if ((res = tree->compar(key, node->data)) == 0)
281
59.9k
      debug_return_ptr(node);
282
66.7k
  node = res < 0 ? node->left : node->right;
283
66.7k
    }
284
65.6k
    debug_return_ptr(NULL);
285
65.6k
}
286
287
/*
288
 * Call func() for each node, passing it the node data and a cookie;
289
 * If func() returns non-zero for a node, the traversal stops and the
290
 * error value is returned.  Returns 0 on successful traversal.
291
 */
292
int
293
rbapply_node(struct rbtree *tree, struct rbnode *node,
294
    int (*func)(void *, void *), void *cookie, enum rbtraversal order)
295
0
{
296
0
    int error;
297
0
    debug_decl(rbapply_node, SUDOERS_DEBUG_RBTREE);
298
299
0
    if (node != rbnil(tree)) {
300
0
  if (order == preorder)
301
0
      if ((error = func(node->data, cookie)) != 0)
302
0
    debug_return_int(error);
303
0
  if ((error = rbapply_node(tree, node->left, func, cookie, order)) != 0)
304
0
      debug_return_int(error);
305
0
  if (order == inorder)
306
0
      if ((error = func(node->data, cookie)) != 0)
307
0
    debug_return_int(error);
308
0
  if ((error = rbapply_node(tree, node->right, func, cookie, order)) != 0)
309
0
      debug_return_int(error);
310
0
  if (order == postorder)
311
0
      if ((error = func(node->data, cookie)) != 0)
312
0
    debug_return_int(error);
313
0
    }
314
0
    debug_return_int(0);
315
0
}
316
317
/*
318
 * Returns the successor of node, or nil if there is none.
319
 */
320
static struct rbnode *
321
rbsuccessor(struct rbtree *tree, struct rbnode *node)
322
0
{
323
0
    struct rbnode *succ;
324
0
    debug_decl(rbsuccessor, SUDOERS_DEBUG_RBTREE);
325
326
0
    if ((succ = node->right) != rbnil(tree)) {
327
0
  while (succ->left != rbnil(tree))
328
0
      succ = succ->left;
329
0
    } else {
330
  /* No right child, move up until we find it or hit the root */
331
0
  for (succ = node->parent; node == succ->right; succ = succ->parent)
332
0
      node = succ;
333
0
  if (succ == rbroot(tree))
334
0
      succ = rbnil(tree);
335
0
    }
336
0
    debug_return_ptr(succ);
337
0
}
338
339
/*
340
 * Recursive portion of rbdestroy().
341
 */
342
static void
343
rbdestroy_int(struct rbtree *tree, struct rbnode *node, void (*destroy)(void *))
344
292k
{
345
292k
    debug_decl(rbdestroy_int, SUDOERS_DEBUG_RBTREE);
346
292k
    if (node != rbnil(tree)) {
347
117k
  rbdestroy_int(tree, node->left, destroy);
348
117k
  rbdestroy_int(tree, node->right, destroy);
349
117k
  if (destroy != NULL)
350
117k
      destroy(node->data);
351
117k
  free(node);
352
117k
    }
353
292k
    debug_return;
354
292k
}
355
356
/*
357
 * Destroy the specified tree, calling the destructor "destroy"
358
 * for each node and then freeing the tree itself.
359
 */
360
void
361
rbdestroy(struct rbtree *tree, void (*destroy)(void *))
362
57.5k
{
363
57.5k
    debug_decl(rbdestroy, SUDOERS_DEBUG_RBTREE);
364
57.5k
    rbdestroy_int(tree, rbfirst(tree), destroy);
365
57.5k
    free(tree);
366
57.5k
    debug_return;
367
57.5k
}
368
369
/*
370
 * Delete node 'z' from the tree and return its data pointer.
371
 */
372
void *rbdelete(struct rbtree *tree, struct rbnode *z)
373
0
{
374
0
    struct rbnode *x, *y;
375
0
    void *data = z->data;
376
0
    debug_decl(rbdelete, SUDOERS_DEBUG_RBTREE);
377
378
0
    if (z->left == rbnil(tree) || z->right == rbnil(tree))
379
0
  y = z;
380
0
    else
381
0
  y = rbsuccessor(tree, z);
382
0
    x = (y->left == rbnil(tree)) ? y->right : y->left;
383
384
0
    if ((x->parent = y->parent) == rbroot(tree)) {
385
0
  rbfirst(tree) = x;
386
0
    } else {
387
0
  if (y == y->parent->left)
388
0
      y->parent->left = x;
389
0
  else
390
0
      y->parent->right = x;
391
0
    }
392
0
    if (y->color == black)
393
0
  rbrepair(tree, x);
394
0
    if (y != z) {
395
0
  y->left = z->left;
396
0
  y->right = z->right;
397
0
  y->parent = z->parent;
398
0
  y->color = z->color;
399
0
  z->left->parent = z->right->parent = y;
400
0
  if (z == z->parent->left)
401
0
      z->parent->left = y; 
402
0
  else
403
0
      z->parent->right = y;
404
0
    }
405
0
    free(z); 
406
    
407
0
    debug_return_ptr(data);
408
0
}
409
410
/*
411
 * Repair the tree after a node has been deleted by rotating and repainting
412
 * colors to restore the 4 properties inherent in red-black trees.
413
 */
414
static void
415
rbrepair(struct rbtree *tree, struct rbnode *node)
416
0
{
417
0
    struct rbnode *sibling;
418
0
    debug_decl(rbrepair, SUDOERS_DEBUG_RBTREE);
419
420
0
    while (node->color == black && node != rbfirst(tree)) {
421
0
  if (node == node->parent->left) {
422
0
      sibling = node->parent->right;
423
0
      if (sibling->color == red) {
424
0
    sibling->color = black;
425
0
    node->parent->color = red;
426
0
    rotate_left(tree, node->parent);
427
0
    sibling = node->parent->right;
428
0
      }
429
0
      if (sibling->right->color == black && sibling->left->color == black) {
430
0
    sibling->color = red;
431
0
    node = node->parent;
432
0
      } else {
433
0
    if (sibling->right->color == black) {
434
0
          sibling->left->color = black;
435
0
          sibling->color = red;
436
0
          rotate_right(tree, sibling);
437
0
          sibling = node->parent->right;
438
0
    }
439
0
    sibling->color = node->parent->color;
440
0
    node->parent->color = black;
441
0
    sibling->right->color = black;
442
0
    rotate_left(tree, node->parent);
443
0
    node = rbfirst(tree); /* exit loop */
444
0
      }
445
0
  } else { /* if (node == node->parent->right) */
446
0
      sibling = node->parent->left;
447
0
      if (sibling->color == red) {
448
0
    sibling->color = black;
449
0
    node->parent->color = red;
450
0
    rotate_right(tree, node->parent);
451
0
    sibling = node->parent->left;
452
0
      }
453
0
      if (sibling->right->color == black && sibling->left->color == black) {
454
0
    sibling->color = red;
455
0
    node = node->parent;
456
0
      } else {
457
0
    if (sibling->left->color == black) {
458
0
        sibling->right->color = black;
459
0
        sibling->color = red;
460
0
        rotate_left(tree, sibling);
461
0
        sibling = node->parent->left;
462
0
    }
463
0
    sibling->color = node->parent->color;
464
0
    node->parent->color = black;
465
0
    sibling->left->color = black;
466
0
    rotate_right(tree, node->parent);
467
0
    node = rbfirst(tree); /* exit loop */
468
0
      }
469
0
  }
470
0
    }
471
0
    node->color = black;
472
473
0
    debug_return;
474
0
}