Skip to content

Commit

Permalink
Fix queryktharr() return value/behaviour. (AMReX-Codes#4186)
Browse files Browse the repository at this point in the history
Up to AMReX 24.07 (at least), ParmParse::queryktharr() used to return 0
if the given k (2nd parameter) was out of range.
As of AMReX 24.10, this is not the case, and ppindex() attempts to
access found->second.m_vals out-of-bounds, resulting in
seg-fault/undefined behaviour.

This PR reverts to the original behaviour, which is now consistent with
the documentation for querykth() in AMReX_ParmParse.H. It also makes
minor corrections to the associated comments to align with this
behaviour.
  • Loading branch information
philip-blakely authored Oct 9, 2024
1 parent e122280 commit bd56c53
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Src/Base/AMReX_ParmParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ getToken (const char*& str, std::string& ostr, int& num_linefeeds)
//
// Return the index of the n'th occurrence of a parameter name,
// except if n==-1, return the index of the last occurrence.
// Return 0 if the specified occurrence does not exist.
// Return nullptr if the specified occurrence does not exist.
//
std::vector<std::string> const*
ppindex (const ParmParse::Table& table, int n, const std::string& name)
Expand All @@ -365,6 +365,9 @@ ppindex (const ParmParse::Table& table, int n, const std::string& name)
if (n == ParmParse::LAST) {
return &(found->second.m_vals.back());
} else {
if(found->second.m_vals.size() < (std::size_t)n + 1) {
return nullptr;
}
return &(found->second.m_vals[n]);
}
}
Expand Down Expand Up @@ -642,7 +645,7 @@ squeryval (const ParmParse::Table& table,
int occurrence)
{
//
// Get last occurrence of name in table.
// Get specified occurrence of name in table.
//
auto const* def = ppindex(table, occurrence, name);
if ( def == nullptr )
Expand Down

0 comments on commit bd56c53

Please sign in to comment.