Skip to content

Commit ef473a7

Browse files
committed
don't access empty vectors
1 parent 4226e10 commit ef473a7

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

highs/ipm/hipo/factorhighs/CliqueStack.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {
6367
void 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));

highs/ipm/hipo/factorhighs/HybridHybridFormatHandler.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "HybridHybridFormatHandler.h"
22

3+
#include <cassert>
34
#include <cstring>
45

56
#include "CallAndTimeBlas.h"
@@ -38,7 +39,11 @@ void HybridHybridFormatHandler::initFrontal() {
3839

3940
void HybridHybridFormatHandler::initClique() {
4041
clique_.resize(S_->cliqueSize(sn_));
41-
clique_ptr_ = clique_.data();
42+
43+
// If the clique size is zero, do not access the underlying pointer. This
44+
// causes strange issues on windows. It's not a problem if clique_ptr_ remains
45+
// null, because it will never be used in that case.
46+
if (!clique_.empty()) clique_ptr_ = clique_.data();
4247
}
4348

4449
void HybridHybridFormatHandler::assembleFrontal(Int i, Int j, double val) {
@@ -72,6 +77,9 @@ void HybridHybridFormatHandler::assembleFrontalMultiple(Int num,
7277
Int HybridHybridFormatHandler::denseFactorise(double reg_thresh) {
7378
Int status;
7479

80+
// either clique is valid, or clique is not needed
81+
assert(clique_ptr_ || ldf_ == sn_size_);
82+
7583
status = denseFactFP2FH(frontal_.data(), ldf_, sn_size_, nb_, data_);
7684
if (status) return status;
7785

0 commit comments

Comments
 (0)