Coverage Report

Created: 2025-11-11 06:08

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