Skip to content

Commit 61a102d

Browse files
committed
Protect TF1 constructor against nullptr as formula parameter
Use more clear std::make_unique calls
1 parent 6fa5c44 commit 61a102d

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

hist/hist/src/TF1.cxx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,16 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, EA
523523
TNamed(name, formula), TAttLine(), TAttFill(), TAttMarker(), fType(EFType::kFormula)
524524
{
525525
if (xmin < xmax) {
526-
fXmin = xmin;
527-
fXmax = xmax;
526+
fXmin = xmin;
527+
fXmax = xmax;
528528
} else {
529-
fXmin = xmax; //when called from TF2,TF3
529+
fXmin = xmax; // when called from TF2,TF3
530530
fXmax = xmin;
531531
}
532532
// Create rep formula (no need to add to gROOT list since we will add the TF1 object)
533-
const auto formulaLength = strlen(formula);
533+
const auto formulaLength = formula ? strlen(formula) : 0;
534534
// First check if we are making a convolution
535-
if (strncmp(formula, "CONV(", 5) == 0 && formula[formulaLength - 1] == ')') {
535+
if (formulaLength > 5 && strncmp(formula, "CONV(", 5) == 0 && formula[formulaLength - 1] == ')') {
536536
// Look for single ',' delimiter
537537
int delimPosition = -1;
538538
int parenCount = 0;
@@ -559,15 +559,15 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, EA
559559
formula2.ReplaceAll(' ', "");
560560

561561
TF1 *function1 = (TF1 *)(gROOT->GetListOfFunctions()->FindObject(formula1));
562-
if (function1 == nullptr)
563-
function1 = new TF1((const char *)formula1, (const char *)formula1, xmin, xmax);
562+
if (!function1)
563+
function1 = new TF1(formula1.Data(), formula1.Data(), xmin, xmax);
564564
TF1 *function2 = (TF1 *)(gROOT->GetListOfFunctions()->FindObject(formula2));
565-
if (function2 == nullptr)
566-
function2 = new TF1((const char *)formula2, (const char *)formula2, xmin, xmax);
565+
if (!function2)
566+
function2 = new TF1(formula2.Data(), formula2.Data(), xmin, xmax);
567567

568568
// std::cout << "functions have been defined" << std::endl;
569569

570-
TF1Convolution *conv = new TF1Convolution(function1, function2,xmin,xmax);
570+
TF1Convolution *conv = new TF1Convolution(function1, function2, xmin, xmax);
571571

572572
// (note: currently ignoring `useFFT` option)
573573
fNpar = conv->GetNpar();
@@ -576,7 +576,7 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, EA
576576
fType = EFType::kCompositionFcn;
577577
fComposition = std::unique_ptr<TF1AbsComposition>(conv);
578578

579-
fParams = std::unique_ptr<TF1Parameters>(new TF1Parameters(fNpar)); // default to zeros (TF1Convolution has no GetParameters())
579+
fParams = std::make_unique<TF1Parameters>(fNpar); // default to zeros (TF1Convolution has no GetParameters())
580580
// set parameter names
581581
for (int i = 0; i < fNpar; i++)
582582
this->SetParName(i, conv->GetParName(i));
@@ -605,7 +605,7 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, EA
605605
}
606606

607607
// Then check if we need NSUM syntax:
608-
} else if (strncmp(formula, "NSUM(", 5) == 0 && formula[formulaLength - 1] == ')') {
608+
} else if (formulaLength > 5 && strncmp(formula, "NSUM(", 5) == 0 && formula[formulaLength - 1] == ')') {
609609
// using comma as delimiter
610610
char delimiter = ',';
611611
// first, remove "NSUM(" and ")" and spaces
@@ -646,21 +646,21 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, EA
646646
fType = EFType::kCompositionFcn;
647647
fComposition = std::unique_ptr<TF1AbsComposition>(normSum);
648648

649-
fParams = std::unique_ptr<TF1Parameters>(new TF1Parameters(fNpar));
649+
fParams = std::make_unique<TF1Parameters>(fNpar);
650650
fParams->SetParameters(&(normSum->GetParameters())[0]); // inherit default parameters from normSum
651651

652652
// Parameter names
653653
for (int i = 0; i < fNpar; i++) {
654-
if (coeffNames->At(i) != nullptr) {
654+
if (coeffNames->At(i)) {
655655
TString coeffName = ((TObjString *)coeffNames->At(i))->GetString();
656-
this->SetParName(i, (const char *)coeffName);
656+
this->SetParName(i, coeffName.Data());
657657
} else {
658658
this->SetParName(i, normSum->GetParName(i));
659659
}
660660
}
661661

662662
} else { // regular TFormula
663-
fFormula = std::unique_ptr<TFormula>(new TFormula(name, formula, false, vectorize));
663+
fFormula = std::make_unique<TFormula>(name, formula, false, vectorize);
664664
fNpar = fFormula->GetNpar();
665665
// TFormula can have dimension zero, but since this is a TF1 minimal dim is 1
666666
fNdim = fFormula->GetNdim() == 0 ? 1 : fFormula->GetNdim();
@@ -679,21 +679,26 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, EA
679679

680680
DoInitialize(addToGlobList);
681681
}
682-
TF1::EAddToList GetGlobalListOption(Option_t * opt) {
682+
683+
TF1::EAddToList GetGlobalListOption(Option_t * opt)
684+
{
683685
if (opt == nullptr) return TF1::EAddToList::kDefault;
684686
TString option(opt);
685687
option.ToUpper();
686688
if (option.Contains("NL")) return TF1::EAddToList::kNo;
687689
if (option.Contains("GL")) return TF1::EAddToList::kAdd;
688690
return TF1::EAddToList::kDefault;
689691
}
690-
bool GetVectorizedOption(Option_t * opt) {
691-
if (opt == nullptr) return false;
692+
693+
bool GetVectorizedOption(Option_t * opt)
694+
{
695+
if (!opt) return false;
692696
TString option(opt);
693697
option.ToUpper();
694698
if (option.Contains("VEC")) return true;
695699
return false;
696700
}
701+
697702
TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, Option_t * opt) :
698703
////////////////////////////////////////////////////////////////////////////////
699704
/// Same constructor as above (for TFormula based function) but passing an option strings
@@ -705,6 +710,7 @@ TF1::TF1(const char *name, const char *formula, Double_t xmin, Double_t xmax, Op
705710
///////////////////////////////////////////////////////////////////////////////////
706711
TF1(name, formula, xmin, xmax, GetGlobalListOption(opt), GetVectorizedOption(opt) )
707712
{}
713+
708714
////////////////////////////////////////////////////////////////////////////////
709715
/// F1 constructor using name of an interpreted function.
710716
///

0 commit comments

Comments
 (0)