Skip to content

Commit f00b04e

Browse files
ferdymercuryvepadulano
authored andcommitted
[hist] prevent TFormula out of bounds access
Fixes #21104 [hist] fix access when opening bracket pos exactly at length [hist] additional out of bounds access safety guards
1 parent 17d0686 commit f00b04e

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

hist/hist/src/TFormula.cxx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ void TFormula::HandleParametrizedFunctions(TString &formula)
12501250
// check if function has specified the [...] e.g. gaus[x,y]
12511251
Int_t openingBracketPos = funPos + funName.Length() + (isNormalized ? 1 : 0);
12521252
Int_t closingBracketPos = kNPOS;
1253-
if (openingBracketPos > formula.Length() || formula[openingBracketPos] != '[') {
1253+
if (openingBracketPos >= formula.Length() || formula[openingBracketPos] != '[') {
12541254
dim = funDim;
12551255
variables.resize(dim);
12561256
for (Int_t idim = 0; idim < dim; ++idim)
@@ -1428,29 +1428,29 @@ void TFormula::HandleFunctionArguments(TString &formula)
14281428

14291429
// ignore things that start with square brackets
14301430
if (formula[i] == '[') {
1431-
while (formula[i] != ']')
1431+
while (i < formula.Length() && formula[i] != ']')
14321432
i++;
14331433
continue;
14341434
}
14351435
// ignore strings
1436-
if (formula[i] == '\"') {
1436+
if (i < formula.Length() && formula[i] == '\"') {
14371437
do
14381438
i++;
1439-
while (formula[i] != '\"');
1439+
while (i < formula.Length() && formula[i] != '\"');
14401440
continue;
14411441
}
14421442
// ignore numbers (scientific notation)
14431443
if (IsScientificNotation(formula, i))
14441444
continue;
14451445
// ignore x in hexadecimal number
14461446
if (IsHexadecimal(formula, i)) {
1447-
while (!IsOperator(formula[i]) && i < formula.Length())
1447+
while (i < formula.Length() && !IsOperator(formula[i]))
14481448
i++;
14491449
continue;
14501450
}
14511451

14521452
// investigate possible start of function name
1453-
if (isalpha(formula[i]) && !IsOperator(formula[i])) {
1453+
if (i < formula.Length() && isalpha(formula[i]) && !IsOperator(formula[i])) {
14541454
// std::cout << "character : " << i << " " << formula[i] << " is not an operator and is alpha" << std::endl;
14551455

14561456
int j; // index to end of name
@@ -1956,7 +1956,7 @@ void TFormula::ExtractFunctors(TString &formula)
19561956
// look for next instance of "\"
19571957
do {
19581958
i++;
1959-
} while (formula[i] != '\"');
1959+
} while (i < formula.Length() && formula[i] != '\"');
19601960
}
19611961
// case of e or E for numbers in exponential notation (e.g. 2.2e-3)
19621962
if (IsScientificNotation(formula, i))
@@ -1965,7 +1965,7 @@ void TFormula::ExtractFunctors(TString &formula)
19651965
if (IsHexadecimal(formula, i)) {
19661966
// find position of operator
19671967
// do not check cases if character is not only a to f, but accept anything
1968-
while (!IsOperator(formula[i]) && i < formula.Length()) {
1968+
while (i < formula.Length() && !IsOperator(formula[i])) {
19691969
i++;
19701970
}
19711971
continue;
@@ -1997,7 +1997,7 @@ void TFormula::ExtractFunctors(TString &formula)
19971997
// printf(" build a name %s \n",name.Data() );
19981998
if (formula[i] == '(') {
19991999
i++;
2000-
if (formula[i] == ')') {
2000+
if (i < formula.Length() && formula[i] == ')') {
20012001
fFuncs.push_back(TFormulaFunction(name, body, 0));
20022002
name = body = "";
20032003
continue;
@@ -3622,8 +3622,8 @@ TString TFormula::GetExpFormula(Option_t *option) const
36223622
// look for p[number
36233623
if (clingFormula[i] == 'p' && clingFormula[i+1] == '[' && isdigit(clingFormula[i+2]) ) {
36243624
int j = i+3;
3625-
while ( isdigit(clingFormula[j]) ) { j++;}
3626-
if (clingFormula[j] != ']') {
3625+
while ( j < clingFormula.Length() && isdigit(clingFormula[j]) ) { j++;}
3626+
if ( j >= clingFormula.Length() || clingFormula[j] != ']') {
36273627
Error("GetExpFormula","Parameters not found - invalid expression - return default cling formula");
36283628
return clingFormula;
36293629
}
@@ -3647,8 +3647,8 @@ TString TFormula::GetExpFormula(Option_t *option) const
36473647
// look for [parName]
36483648
if (expFormula[i] == '[') {
36493649
int j = i+1;
3650-
while ( expFormula[j] != ']' ) { j++;}
3651-
if (expFormula[j] != ']') {
3650+
while ( j < expFormula.Length() && expFormula[j] != ']' ) { j++;}
3651+
if ( j >= expFormula.Length() || expFormula[j] != ']') {
36523652
Error("GetExpFormula","Parameter names not found - invalid expression - return default formula");
36533653
return expFormula;
36543654
}

0 commit comments

Comments
 (0)