Thread (2 messages) 2 messages, 2 authors, 2017-05-24

Re: [block] question about potential null pointer dereference

From: Paolo Valente <hidden>
Date: 2017-05-24 11:34:31
Also in: lkml

Il giorno 23 mag 2017, alle ore 22:52, Gustavo A. R. Silva =
[off-list ref] ha scritto:
=20
=20
Hello everybody,
=20
Hi
While looking into Coverity ID 1408828 I ran into the following piece =
of code at block/bfq-wf2q.c:542:
=20
542static struct rb_node *bfq_find_deepest(struct rb_node *node)
543{
544        struct rb_node *deepest;
545
546        if (!node->rb_right && !node->rb_left)
547                deepest =3D rb_parent(node);
548        else if (!node->rb_right)
549                deepest =3D node->rb_left;
550        else if (!node->rb_left)
551                deepest =3D node->rb_right;
552        else {
553                deepest =3D rb_next(node);
554                if (deepest->rb_right)
555                        deepest =3D deepest->rb_right;
556                else if (rb_parent(deepest) !=3D node)
557                        deepest =3D rb_parent(deepest);
558        }
559
560        return deepest;
561}
=20
The issue here is that there is a potential NULL pointer dereference =
at line 554, in case function rb_next() returns NULL.
=20
Can rb_next(node) return NULL, although node is guaranteed to have both
a left and a right child at line 554?

Thanks,
Paolo
Maybe a patch like the following could be applied in order to avoid =
any chance of a NULL pointer dereference:
quoted hunk ↗ jump to hunk
=20
index 8726ede..28d8b90 100644
--- a/block/bfq-wf2q.c
+++ b/block/bfq-wf2q.c
@@ -551,6 +551,8 @@ static struct rb_node *bfq_find_deepest(struct =
rb_node *node)
               deepest =3D node->rb_right;
       else {
               deepest =3D rb_next(node);
+               if (!deepest)
+                       return NULL;
               if (deepest->rb_right)
                       deepest =3D deepest->rb_right;
               else if (rb_parent(deepest) !=3D node)
=20
What do you think?
=20
I'd really appreciate any comment on this.
=20
Thank you!
--
Gustavo A. R. Silva
=20
=20
=20
=20
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help