Commit bc4d1b60 authored by Staffan Gimåker's avatar Staffan Gimåker

Fixed segfault bug sometimes happening when pruning the trie/mtrie.

The cause behind the segfault was next.node being deleted but count still
being non-zero.
Signed-off-by: 's avatarStaffan Gimåker <staffan@spotify.com>
parent 19129edc
...@@ -42,8 +42,11 @@ zmq::mtrie_t::mtrie_t () : ...@@ -42,8 +42,11 @@ zmq::mtrie_t::mtrie_t () :
zmq::mtrie_t::~mtrie_t () zmq::mtrie_t::~mtrie_t ()
{ {
if (count == 1) if (count == 1) {
zmq_assert (next.node);
delete next.node; delete next.node;
next.node = 0;
}
else if (count > 1) { else if (count > 1) {
for (unsigned short i = 0; i != count; ++i) for (unsigned short i = 0; i != count; ++i)
if (next.table [i]) if (next.table [i])
...@@ -174,6 +177,7 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, ...@@ -174,6 +177,7 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
if (next.node->is_redundant ()) { if (next.node->is_redundant ()) {
delete next.node; delete next.node;
next.node = 0; next.node = 0;
count = 0;
--live_nodes; --live_nodes;
} }
return; return;
...@@ -182,13 +186,14 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, ...@@ -182,13 +186,14 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
// If there are multiple subnodes. // If there are multiple subnodes.
for (unsigned short c = 0; c != count; c++) { for (unsigned short c = 0; c != count; c++) {
(*buff_) [buffsize_] = min + c; (*buff_) [buffsize_] = min + c;
if (next.table [c]) if (next.table [c]) {
next.table [c]->rm_helper (pipe_, buff_, buffsize_ + 1, next.table [c]->rm_helper (pipe_, buff_, buffsize_ + 1,
maxbuffsize_, func_, arg_); maxbuffsize_, func_, arg_);
if (next.table [c]->is_redundant ()) { if (next.table [c]->is_redundant ()) {
delete next.table [c]; delete next.table [c];
next.table [c] = 0; next.table [c] = 0;
--live_nodes; --live_nodes;
}
} }
} }
} }
...@@ -221,8 +226,10 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_, ...@@ -221,8 +226,10 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
if (next_node->is_redundant ()) { if (next_node->is_redundant ()) {
delete next_node; delete next_node;
if (count == 1) if (count == 1) {
next.node = 0; next.node = 0;
count = 0;
}
else else
next.table [c - min] = 0; next.table [c - min] = 0;
--live_nodes; --live_nodes;
...@@ -272,7 +279,7 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_, ...@@ -272,7 +279,7 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_,
} }
} }
bool zmq::mtrie_t::is_redundant() const bool zmq::mtrie_t::is_redundant () const
{ {
return pipes.empty () && live_nodes == 0; return pipes.empty () && live_nodes == 0;
} }
...@@ -43,8 +43,11 @@ zmq::trie_t::trie_t () : ...@@ -43,8 +43,11 @@ zmq::trie_t::trie_t () :
zmq::trie_t::~trie_t () zmq::trie_t::~trie_t ()
{ {
if (count == 1) if (count == 1) {
zmq_assert (next.node);
delete next.node; delete next.node;
next.node = 0;
}
else if (count > 1) { else if (count > 1) {
for (unsigned short i = 0; i != count; ++i) for (unsigned short i = 0; i != count; ++i)
if (next.table [i]) if (next.table [i])
...@@ -154,8 +157,10 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_) ...@@ -154,8 +157,10 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
if (next_node->is_redundant ()) { if (next_node->is_redundant ()) {
delete next_node; delete next_node;
if (count == 1) if (count == 1) {
next.node = 0; next.node = 0;
count = 0;
}
else else
next.table [c - min] = 0; next.table [c - min] = 0;
--live_nodes; --live_nodes;
...@@ -242,7 +247,7 @@ void zmq::trie_t::apply_helper ( ...@@ -242,7 +247,7 @@ void zmq::trie_t::apply_helper (
} }
} }
bool zmq::trie_t::is_redundant() const bool zmq::trie_t::is_redundant () const
{ {
return refcnt == 0 && live_nodes == 0; return refcnt == 0 && live_nodes == 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment