@@ -20,19 +20,23 @@ double* CliqueStack::setup(Int64 clique_size, bool& reallocation) {
2020 assert (!workspace_ && !worksize_);
2121 reallocation = false ;
2222
23- if (stack_.size () > 0 ) {
23+ if (! stack_.empty () ) {
2424 // This should not trigger reallocation, because the resize in init is done
2525 // with the maximum possible size of the stack.
2626 if (top_ + clique_size > stack_.size ()) {
2727 reallocation = true ;
2828 stack_.resize (top_ + clique_size, 0.0 );
2929 }
3030
31- workspace_ = &stack_[top_];
32- worksize_ = clique_size;
31+ if (clique_size > 0 ) {
32+ // accessing stack[top] is valid only if the clique is not empty,
33+ // otherwise it may be out of bounds.
34+ workspace_ = &stack_[top_];
35+ worksize_ = clique_size;
3336
34- // initialize workspace to zero
35- std::memset (workspace_, 0 , worksize_ * sizeof (double ));
37+ // initialize workspace to zero
38+ std::memset (workspace_, 0 , worksize_ * sizeof (double ));
39+ }
3640 }
3741
3842 return workspace_;
@@ -63,7 +67,7 @@ void CliqueStack::popChild() {
6367void CliqueStack::pushWork (Int sn) {
6468 // Put the content of the workspace at the top of the stack
6569
66- if (stack_.size () > 0 ) {
70+ if (! stack_.empty () ) {
6771 // stack_[top_] has lower address than workspace, so no need to resize.
6872 // workspace_ and stack_[top_] do not overlap, so use memcpy
6973 std::memcpy (&stack_[top_], workspace_, worksize_ * sizeof (double ));
0 commit comments