git: 882c35e41275 - stable/13 - rb_tree: silence coverity

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Mon, 01 Aug 2022 05:45:38 UTC
The branch stable/13 has been updated by dougm:

URL: https://cgit.FreeBSD.org/src/commit/?id=882c35e41275c1eb98a5a8f3444ac9f09f292e3c

commit 882c35e41275c1eb98a5a8f3444ac9f09f292e3c
Author:     Doug Moore <dougm@FreeBSD.org>
AuthorDate: 2022-06-30 17:27:33 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2022-08-01 05:37:37 +0000

    rb_tree: silence coverity
    
    Add comments to RB_INSERT_COLOR to silence coverity warnings about the
    use of an uninitialized variable.  Since other static analyzers will
    complain too, add a comment to explain why the complaints are unwarranted.
    
    Reviewed by:    markj
    MFC after:      3 weeks
    Differential Revision:  https://reviews.freebsd.org/D35671
    
    (cherry picked from commit 91b30f7ad22642979b56a56b211843cbd9d35984)
---
 sys/sys/tree.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/sys/sys/tree.h b/sys/sys/tree.h
index 4cdd9548ba62..2206469db492 100644
--- a/sys/sys/tree.h
+++ b/sys/sys/tree.h
@@ -464,6 +464,17 @@ struct {								\
 attr void								\
 name##_RB_INSERT_COLOR(struct name *head, struct type *elm)		\
 {									\
+	/*								\
+	 * Initially, elm is a leaf.  Either its parent was previously	\
+	 * a leaf, with two black null children, or an interior node	\
+	 * with a black non-null child and a red null child. The        \
+	 * balance criterion "the rank of any leaf is 1" precludes the  \
+	 * possibility of two red null children for the initial parent. \
+	 * So the first loop iteration cannot lead to accessing an      \
+	 * uninitialized 'child', and a later iteration can only happen \
+	 * when a value has been assigned to 'child' in the previous    \
+	 * one.								\
+	 */								\
 	struct type *child, *parent;					\
 	while ((parent = RB_PARENT(elm, field)) != NULL) {		\
 		if (RB_LEFT(parent, field) == elm) {			\
@@ -478,6 +489,7 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm)		\
 			}						\
 			if (!RB_RED_RIGHT(elm, field)) {		\
 				RB_FLIP_LEFT(elm, field);		\
+				/* coverity[uninit_use] */		\
 				RB_ROTATE_LEFT(head, elm, child, field);\
 				if (RB_RED_LEFT(child, field))		\
 					RB_FLIP_RIGHT(elm, field);	\
@@ -498,6 +510,7 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm)		\
 			}						\
 			if (!RB_RED_LEFT(elm, field)) {			\
 				RB_FLIP_RIGHT(elm, field);		\
+				/* coverity[uninit_use] */		\
 				RB_ROTATE_RIGHT(head, elm, child, field);\
 				if (RB_RED_RIGHT(child, field))		\
 					RB_FLIP_LEFT(elm, field);	\