From bd56c535eb84a0e2996706c4c10424b5f7469db3 Mon Sep 17 00:00:00 2001 From: philip-blakely <46958218+philip-blakely@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:49:26 +0100 Subject: [PATCH] Fix queryktharr() return value/behaviour. (#4186) 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. --- Src/Base/AMReX_ParmParse.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index 34d383c99a..3ecfc8503a 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -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 const* ppindex (const ParmParse::Table& table, int n, const std::string& name) @@ -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]); } } @@ -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 )