From cf14cb3e29d41097e34c911d2fe531c6ed60dc53 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Fri, 19 Feb 2021 13:42:05 -0800 Subject: [PATCH] Avoid self-move-assign in pop operation of binary heap. (#7942) Summary: The current implementation of a binary heap in `util/heap.h` does a move-assign in the `pop` method. In the case that there is exactly one element stored in the heap, this ends up being a self-move-assign. This can cause trouble with certain classes, which are not prepared for this. Furthermore, it trips up the glibc STL debugger (`-D_GLIBCXX_DEBUG`), which produces an assertion failure in this case. This PR addresses this problem by not doing the (unnecessary in this case) move-assign if there is only one element in the heap. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7942 Reviewed By: jay-zhuang Differential Revision: D26528739 Pulled By: ajkr fbshipit-source-id: 5ca570e0c4168f086b10308ad766dff84e6e2d03 --- util/heap.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/util/heap.h b/util/heap.h index c72d944d3..e0737581e 100644 --- a/util/heap.h +++ b/util/heap.h @@ -72,7 +72,12 @@ class BinaryHeap { void pop() { assert(!empty()); - data_.front() = std::move(data_.back()); + if (data_.size() > 1) { + // Avoid self-move-assign, because it could cause problems with + // classes which are not prepared for this and it trips up the + // STL debugger when activated. + data_.front() = std::move(data_.back()); + } data_.pop_back(); if (!empty()) { downheap(get_root());