From: xie xingguo Date: Mon, 8 Aug 2016 07:49:39 +0000 (+0800) Subject: common/WPQ: add sanity check to avoid potential access violation X-Git-Tag: v11.0.1~410^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5c707ad03b8780f30ed6b7de36f0156faacff8be;p=ceph.git common/WPQ: add sanity check to avoid potential access violation For dump command, it will be the normal case that rbtree is empty(), so get_cost() is at risk of accessing violation. Signed-off-by: xie xingguo --- diff --git a/src/common/WeightedPriorityQueue.h b/src/common/WeightedPriorityQueue.h index a94ff2d3094..bef8bc01dbc 100644 --- a/src/common/WeightedPriorityQueue.h +++ b/src/common/WeightedPriorityQueue.h @@ -84,7 +84,8 @@ class WeightedPriorityQueue : public OpQueue } //Get the cost of the next item to dequeue unsigned get_cost() const { - return lp.begin()->cost; + assert(!empty()); + return lp.begin()->cost; } T pop() { assert(!lp.empty()); @@ -166,7 +167,8 @@ class WeightedPriorityQueue : public OpQueue ret.first->insert(cost, item, front); } unsigned get_cost() const { - return next->get_cost(); + assert(!empty()); + return next->get_cost(); } T pop() { T ret = next->pop(); @@ -211,8 +213,10 @@ class WeightedPriorityQueue : public OpQueue return count; } void dump(ceph::Formatter *f) const { - f->dump_int("num_keys", next->get_size()); - f->dump_int("first_item_cost", next->get_cost()); + f->dump_int("num_keys", next->get_size()); + if (!empty()) { + f->dump_int("first_item_cost", next->get_cost()); + } } }; class Queue {