Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix undefined behavour due to memcpy(ptr, NULL, 0) #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 13 additions & 40 deletions src/CglCommon/CglTreeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,25 +1013,13 @@ CglTreeProbingInfo::analyze(const OsiSolverInterface &si, int createSolver,
nStrengthen++;
if (numberEntries + jCount + 1 > maximumEntries) {
maximumEntries = CoinMax(numberEntries + jCount + 1, (maximumEntries * 12) / 10 + 100);
CliqueEntry *temp = new CliqueEntry[maximumEntries];
memcpy(temp, entry, numberEntries * sizeof(CliqueEntry));
delete[] entry;
entry = temp;
int *tempI = new int[maximumEntries];
memcpy(tempI, whichClique, numberEntries * sizeof(int));
delete[] whichClique;
whichClique = tempI;
append(entry, numberEntries, maximumEntries);
append(whichClique, numberEntries, maximumEntries);
}
if (numberCliques == maximumCliques) {
maximumCliques = (maximumCliques * 12) / 10 + 100;
CoinBigIndex *temp = new CoinBigIndex[maximumCliques + 1];
memcpy(temp, cliqueStart, (numberCliques + 1) * sizeof(CoinBigIndex));
delete[] cliqueStart;
cliqueStart = temp;
char *tempT = new char[maximumCliques];
memcpy(tempT, cliqueType, numberCliques);
delete[] cliqueType;
cliqueType = tempT;
append(cliqueStart, numberCliques + 1, maximumCliques + 1);
append(cliqueType, numberCliques, maximumCliques);
}
CliqueEntry eI;
eI.fixes = 0;
Expand Down Expand Up @@ -1102,25 +1090,14 @@ CglTreeProbingInfo::analyze(const OsiSolverInterface &si, int createSolver,
nStrengthen++;
if (numberEntries + jCount + 1 > maximumEntries) {
maximumEntries = CoinMax(numberEntries + jCount + 1, (maximumEntries * 12) / 10 + 100);
CliqueEntry *temp = new CliqueEntry[maximumEntries];
memcpy(temp, entry, numberEntries * sizeof(CliqueEntry));
delete[] entry;
entry = temp;
int *tempI = new int[maximumEntries];
memcpy(tempI, whichClique, numberEntries * sizeof(int));
delete[] whichClique;
whichClique = tempI;
append(entry, numberEntries, maximumEntries);
append(whichClique, numberEntries, maximumEntries);
}
if (numberCliques == maximumCliques) {
maximumCliques = (maximumCliques * 12) / 10 + 100;
CoinBigIndex *temp = new CoinBigIndex[maximumCliques + 1];
memcpy(temp, cliqueStart, (numberCliques + 1) * sizeof(CoinBigIndex));
delete[] cliqueStart;
cliqueStart = temp;
char *tempT = new char[maximumCliques];
memcpy(tempT, cliqueType, numberCliques);
delete[] cliqueType;
cliqueType = tempT;

append(cliqueStart, numberCliques + 1, maximumCliques + 1);
append(cliqueType, numberCliques, maximumCliques);
}
CliqueEntry eI;
eI.fixes = 0;
Expand Down Expand Up @@ -1232,6 +1209,7 @@ CglTreeProbingInfo::analyze(const OsiSolverInterface &si, int createSolver,
delete[] whichClique;
return newSolver;
}

// Take action if cut generator can fix a variable (toValue -1 for down, +1 for up)
bool CglTreeProbingInfo::fixes(int variable, int toValue, int fixedVariable, bool fixedToLower)
{
Expand All @@ -1248,14 +1226,9 @@ bool CglTreeProbingInfo::fixes(int variable, int toValue, int fixedVariable, boo
if (maximumEntries_ >= CoinMax(1000000, 10 * numberIntegers_))
return false;
maximumEntries_ += 100 + maximumEntries_ / 2;
CliqueEntry *temp1 = new CliqueEntry[maximumEntries_];
memcpy(temp1, fixEntry_, numberEntries_ * sizeof(CliqueEntry));
delete[] fixEntry_;
fixEntry_ = temp1;
int *temp2 = new int[maximumEntries_];
memcpy(temp2, fixingEntry_, numberEntries_ * sizeof(int));
delete[] fixingEntry_;
fixingEntry_ = temp2;

append(fixEntry_, numberEntries_, maximumEntries_);
append(fixingEntry_, numberEntries_, maximumEntries_);
}
CliqueEntry entry1;
entry1.fixes = 0;
Expand Down
14 changes: 14 additions & 0 deletions src/CglCommon/CglTreeInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
#include "CoinHelperFunctions.hpp"
#include "CglConfig.h"

template<typename T>
void append(T*& array, int oldLength, int newLength)
{
assert(oldLength < newLength);

T* temp = new T[newLength];
if (array != NULL) {
memcpy(temp, array, oldLength * sizeof(T));
delete[] array;
}

array = temp;
}

class CglStored;
/** Information about where the cut generator is invoked from. */

Expand Down
9 changes: 4 additions & 5 deletions src/CglPreProcess/CglPreProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,9 @@ CglPreProcess::preProcessNonDefault(OsiSolverInterface &model,
int nAdd = ((tuning & (64 + USECGLCLIQUE)) == 64 + USECGLCLIQUE && allPlusOnes) ? 2 : 1;
CglCutGenerator **temp = generator_;
generator_ = new CglCutGenerator *[numberCutGenerators_ + nAdd];
memcpy(generator_ + nAdd, temp, numberCutGenerators_ * sizeof(CglCutGenerator *));
if(temp != NULL) {
memcpy(generator_ + nAdd, temp, numberCutGenerators_ * sizeof(CglCutGenerator *));
}
delete[] temp;
numberCutGenerators_ += nAdd;
if (nAdd == 2 || (tuning & USECGLCLIQUE) != 0) {
Expand Down Expand Up @@ -7938,10 +7940,7 @@ void CglPreProcess::gutsOfDestructor()
// Add one generator
void CglPreProcess::addCutGenerator(CglCutGenerator *generator)
{
CglCutGenerator **temp = generator_;
generator_ = new CglCutGenerator *[numberCutGenerators_ + 1];
memcpy(generator_, temp, numberCutGenerators_ * sizeof(CglCutGenerator *));
delete[] temp;
append(generator_, numberCutGenerators_, numberCutGenerators_ + 1);
generator_[numberCutGenerators_++] = generator->clone();
}
//#############################################################################
Expand Down