Skip to content

Commit

Permalink
[hist] add bounds and overflow checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdymercury committed Apr 2, 2024
1 parent 38ed8e8 commit 8039d84
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions hist/hist/inc/TPrincipal.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TPrincipal : public TNamed {
public:
TPrincipal();
~TPrincipal() override;
TPrincipal(Int_t nVariables, Option_t *opt="ND");
TPrincipal(Long64_t nVariables, Option_t *opt="ND");

virtual void AddRow(const Double_t *x);
void Browse(TBrowser *b) override;
Expand All @@ -62,7 +62,7 @@ class TPrincipal : public TNamed {
const TMatrixD *GetEigenVectors() const {return &fEigenVectors;}
TList *GetHistograms() const {return fHistograms;}
const TVectorD *GetMeanValues() const {return &fMeanValues;}
const Double_t *GetRow(Int_t row);
const Double_t *GetRow(Long64_t row);
const TVectorD *GetSigmas() const {return &fSigmas;}
const TVectorD *GetUserData() const {return &fUserData;}
Bool_t IsFolder() const override { return kTRUE;}
Expand Down
20 changes: 16 additions & 4 deletions hist/hist/src/TPrincipal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ TPrincipal::TPrincipal()
///
/// The created object is named "principal" by default.

TPrincipal::TPrincipal(Int_t nVariables, Option_t *opt)
TPrincipal::TPrincipal(Long64_t nVariables, Option_t *opt)
: fMeanValues(nVariables),
fSigmas(nVariables),
fCovarianceMatrix(nVariables,nVariables),
Expand All @@ -260,7 +260,11 @@ TPrincipal::TPrincipal(Int_t nVariables, Option_t *opt)
fStoreData(kFALSE)
{
if (nVariables <= 1) {
Error("TPrincipal", "You can't be serious - nVariables == 1!!!");
Error("TPrincipal", "You can't be serious - nVariables <= 1!!!");
return;
}
if (nVariables > std::numeric_limits<Int_t>::max()) {
Error("TPrincipal", "`nVariables` input parameter %lld is larger than the allowed maximum %d", nVariables, std::numeric_limits<Int_t>::max());
return;
}

Expand Down Expand Up @@ -411,6 +415,10 @@ void TPrincipal::AddRow(const Double_t *p)
{
if (!p)
return;
if (fNumberOfDataPoints == std::numeric_limits<Int_t>::max()) {
Error("AddRow", "`fNumberOfDataPoints` has reached its allowed maximum %d, cannot add new row.", fNumberOfDataPoints);
return;
}

// Increment the data point counter
Int_t i,j;
Expand Down Expand Up @@ -510,15 +518,19 @@ void TPrincipal::Clear(Option_t *opt)
/// It's up to the user to delete the returned array.
/// Row 0 is the first row;

const Double_t *TPrincipal::GetRow(Int_t row)
const Double_t *TPrincipal::GetRow(Long64_t row)
{
if (row >= fNumberOfDataPoints)
return nullptr;

if (!fStoreData)
return nullptr;

Int_t index = row * fNumberOfVariables;
Long64_t index = row * fNumberOfVariables;
if (index > std::numeric_limits<Int_t>::max()) {
Error("GetRow", "Input parameter `row` %lld x fNumberOfVariables %d goes into overflow (%lld>%d), returning nullptr.", row, fNumberOfVariables, index, std::numeric_limits<Int_t>::max());
return nullptr;
}
return &fUserData(index);
}

Expand Down

0 comments on commit 8039d84

Please sign in to comment.