From e4cbc353524f5d76e1b4c2ab3078a4623466a3a7 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 16 May 2021 00:54:15 -0700 Subject: [PATCH] common: tree: Avoid a crash on nullptr dereference. --- src/common/tree.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/common/tree.h b/src/common/tree.h index 9d2d0df4e7..df88e20f4d 100644 --- a/src/common/tree.h +++ b/src/common/tree.h @@ -43,6 +43,8 @@ * The maximum height of a red-black tree is 2lg (n+1). */ +#include "common/assert.h" + namespace Common { template class RBHead { @@ -325,6 +327,10 @@ void RB_REMOVE_COLOR(RBHead* head, Node* parent, Node* elm) { while ((elm == nullptr || RB_IS_BLACK(elm)) && elm != head->Root() && parent != nullptr) { if (RB_LEFT(parent) == elm) { tmp = RB_RIGHT(parent); + if (!tmp) { + ASSERT_MSG(false, "tmp is invalid!"); + break; + } if (RB_IS_RED(tmp)) { RB_SET_BLACKRED(tmp, parent); RB_ROTATE_LEFT(head, parent, tmp);