Coverage Report

Created: 2025-10-10 07:07

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
14.3k
{
84
14.3k
    struct rbtree *tree;
85
14.3k
    debug_decl(rbcreate, SUDOERS_DEBUG_RBTREE);
86
87
14.3k
    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
14.3k
    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
14.3k
    tree->nil.left = tree->nil.right = tree->nil.parent = &tree->nil;
100
14.3k
    tree->nil.color = black;
101
14.3k
    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
14.3k
    tree->root.left = tree->root.right = tree->root.parent = &tree->nil; // -V778
108
14.3k
    tree->root.color = black;
109
14.3k
    tree->root.data = NULL;
110
111
14.3k
    debug_return_ptr(tree);
112
14.3k
}
113
114
/*
115
 * Perform a left rotation starting at node.
116
 */
117
static void
118
rotate_left(struct rbtree *tree, struct rbnode *node)
119
6.74k
{
120
6.74k
    struct rbnode *child;
121
6.74k
    debug_decl(rotate_left, SUDOERS_DEBUG_RBTREE);
122
123
6.74k
    child = node->right;
124
6.74k
    node->right = child->left;
125
126
6.74k
    if (child->left != rbnil(tree))
127
1.87k
        child->left->parent = node;
128
6.74k
    child->parent = node->parent;
129
130
6.74k
    if (node == node->parent->left)
131
4.62k
  node->parent->left = child;
132
2.12k
    else
133
2.12k
  node->parent->right = child;
134
6.74k
    child->left = node;
135
6.74k
    node->parent = child;
136
137
6.74k
    debug_return;
138
6.74k
}
139
140
/*
141
 * Perform a right rotation starting at node.
142
 */
143
static void
144
rotate_right(struct rbtree *tree, struct rbnode *node)
145
6.24k
{
146
6.24k
    struct rbnode *child;
147
6.24k
    debug_decl(rotate_right, SUDOERS_DEBUG_RBTREE);
148
149
6.24k
    child = node->left;
150
6.24k
    node->left = child->right;
151
152
6.24k
    if (child->right != rbnil(tree))
153
1.65k
        child->right->parent = node;
154
6.24k
    child->parent = node->parent;
155
156
6.24k
    if (node == node->parent->left)
157
2.16k
  node->parent->left = child;
158
4.07k
    else
159
4.07k
  node->parent->right = child;
160
6.24k
    child->right = node;
161
6.24k
    node->parent = child;
162
163
6.24k
    debug_return;
164
6.24k
}
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
259k
{
174
259k
    struct rbnode *node = rbfirst(tree);
175
259k
    struct rbnode *parent = rbroot(tree);
176
259k
    int res;
177
259k
    debug_decl(rbinsert, SUDOERS_DEBUG_RBTREE);
178
179
    /* Find correct insertion point. */
180
526k
    while (node != rbnil(tree)) {
181
497k
  parent = node;
182
497k
  if ((res = tree->compar(data, node->data)) == 0) {
183
229k
      if (existing != NULL)
184
229k
    *existing = node;
185
229k
      debug_return_int(1);
186
229k
  }
187
267k
  node = res < 0 ? node->left : node->right;
188
267k
    }
189
190
29.5k
    node = malloc(sizeof(*node));
191
29.5k
    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
29.5k
    node->data = data;
197
29.5k
    node->left = node->right = rbnil(tree);
198
29.5k
    node->parent = parent;
199
29.5k
    if (parent == rbroot(tree) || tree->compar(data, parent->data) < 0)
200
19.8k
  parent->left = node;
201
9.68k
    else
202
9.68k
  parent->right = node;
203
29.5k
    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
46.9k
    while (node->parent->color == red) {
229
17.3k
  struct rbnode *uncle;
230
17.3k
  if (node->parent == node->parent->parent->left) {
231
8.26k
      uncle = node->parent->parent->right;
232
8.26k
      if (uncle->color == red) {
233
4.18k
    node->parent->color = black;
234
4.18k
    uncle->color = black;
235
4.18k
    node->parent->parent->color = red;
236
4.18k
    node = node->parent->parent;
237
4.18k
      } else /* if (uncle->color == black) */ {
238
4.07k
    if (node == node->parent->right) {
239
2.39k
        node = node->parent;
240
2.39k
        rotate_left(tree, node);
241
2.39k
    }
242
4.07k
    node->parent->color = black;
243
4.07k
    node->parent->parent->color = red;
244
4.07k
    rotate_right(tree, node->parent->parent);
245
4.07k
      }
246
9.10k
  } else { /* if (node->parent == node->parent->parent->right) */
247
9.10k
      uncle = node->parent->parent->left;
248
9.10k
      if (uncle->color == red) {
249
4.75k
    node->parent->color = black;
250
4.75k
    uncle->color = black;
251
4.75k
    node->parent->parent->color = red;
252
4.75k
    node = node->parent->parent;
253
4.75k
      } else /* if (uncle->color == black) */ {
254
4.35k
    if (node == node->parent->left) {
255
2.16k
        node = node->parent;
256
2.16k
        rotate_right(tree, node);
257
2.16k
    }
258
4.35k
    node->parent->color = black;
259
4.35k
    node->parent->parent->color = red;
260
4.35k
    rotate_left(tree, node->parent->parent);
261
4.35k
      }
262
9.10k
  }
263
17.3k
    }
264
29.5k
    rbfirst(tree)->color = black;  /* first node is always black */
265
29.5k
    debug_return_int(0);
266
29.5k
}
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
0
{
275
0
    struct rbnode *node = rbfirst(tree);
276
0
    int res;
277
0
    debug_decl(rbfind, SUDOERS_DEBUG_RBTREE);
278
279
0
    while (node != rbnil(tree)) {
280
0
  if ((res = tree->compar(key, node->data)) == 0)
281
0
      debug_return_ptr(node);
282
0
  node = res < 0 ? node->left : node->right;
283
0
    }
284
0
    debug_return_ptr(NULL);
285
0
}
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
73.5k
{
345
73.5k
    debug_decl(rbdestroy_int, SUDOERS_DEBUG_RBTREE);
346
73.5k
    if (node != rbnil(tree)) {
347
29.5k
  rbdestroy_int(tree, node->left, destroy);
348
29.5k
  rbdestroy_int(tree, node->right, destroy);
349
29.5k
  if (destroy != NULL)
350
29.5k
      destroy(node->data);
351
29.5k
  free(node);
352
29.5k
    }
353
73.5k
    debug_return;
354
73.5k
}
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
14.3k
{
363
14.3k
    debug_decl(rbdestroy, SUDOERS_DEBUG_RBTREE);
364
14.3k
    rbdestroy_int(tree, rbfirst(tree), destroy);
365
14.3k
    free(tree);
366
14.3k
    debug_return;
367
14.3k
}
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
}