From 8d607a6692f195691e2635f8f99ce12600c0b062 Mon Sep 17 00:00:00 2001 From: Philip Blakely Date: Mon, 7 Oct 2024 14:44:58 +0100 Subject: [PATCH 1/5] In AMReX 24.07, 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 commit reverts to the original behaviour, which is now consistent with that for querykth(). It also makes minor corrections to the associated documentation. --- Src/Base/AMReX_ParmParse.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index 34d383c99a..0eabbbd724 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,7 +365,10 @@ ppindex (const ParmParse::Table& table, int n, const std::string& name) if (n == ParmParse::LAST) { return &(found->second.m_vals.back()); } else { - return &(found->second.m_vals[n]); + if(found->second.m_vals.size() < 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 ) From 44fbd5caad4a384a40fefc9f3d973b83ad0f7121 Mon Sep 17 00:00:00 2001 From: Philip Blakely Date: Mon, 7 Oct 2024 15:11:06 +0100 Subject: [PATCH 2/5] Fix tabs. --- Src/Base/AMReX_ParmParse.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index 0eabbbd724..e9120680c9 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -366,7 +366,7 @@ ppindex (const ParmParse::Table& table, int n, const std::string& name) return &(found->second.m_vals.back()); } else { if(found->second.m_vals.size() < n+1) { - return nullptr; + return nullptr; } return &(found->second.m_vals[n]); } From f3fe9f20cff5c0429903d6b027c843486b8def74 Mon Sep 17 00:00:00 2001 From: Philip Blakely Date: Mon, 7 Oct 2024 15:16:49 +0100 Subject: [PATCH 3/5] Fix comparison of (signed) int with size_type by casting. --- Src/Base/AMReX_ParmParse.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index e9120680c9..1a5322d861 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -365,7 +365,7 @@ 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() < n+1) { + if(found->second.m_vals.size() < (std::size_t)(n+1)) { return nullptr; } return &(found->second.m_vals[n]); From c648e2db6c1b2eada3e9c6bc332b1cd39633dab1 Mon Sep 17 00:00:00 2001 From: Philip Blakely Date: Mon, 7 Oct 2024 15:25:35 +0100 Subject: [PATCH 4/5] Fix placing of conversion to avoid loss of precision error. --- Src/Base/AMReX_ParmParse.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index 1a5322d861..d8f1fd70c6 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -365,7 +365,7 @@ 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)) { + if(found->second.m_vals.size() < (std::size_t)n + 1) { return nullptr; } return &(found->second.m_vals[n]); From b4ff5450d9c4c50bbd57c2e5956113075f5e5b06 Mon Sep 17 00:00:00 2001 From: philip-blakely <46958218+philip-blakely@users.noreply.github.com> Date: Wed, 9 Oct 2024 08:12:17 +0100 Subject: [PATCH 5/5] Fix indentation as suggested. Co-authored-by: Weiqun Zhang --- Src/Base/AMReX_ParmParse.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index d8f1fd70c6..3ecfc8503a 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -365,10 +365,10 @@ 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]); + if(found->second.m_vals.size() < (std::size_t)n + 1) { + return nullptr; + } + return &(found->second.m_vals[n]); } }