From 7dbb44ce65a30bdf5c62ab707dfd6ca8284af5a8 Mon Sep 17 00:00:00 2001 From: Norbert Podhorszki Date: Wed, 26 Jan 2022 08:04:25 -0500 Subject: [PATCH] Fix target step calculation avoiding integer overflow --- source/adios2/engine/bp5/BP5Writer.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/source/adios2/engine/bp5/BP5Writer.cpp b/source/adios2/engine/bp5/BP5Writer.cpp index 53af024677..726adc1b82 100644 --- a/source/adios2/engine/bp5/BP5Writer.cpp +++ b/source/adios2/engine/bp5/BP5Writer.cpp @@ -682,14 +682,16 @@ uint64_t BP5Writer::CountStepsInMetadataIndex(format::BufferSTL &bufferSTL) } unsigned int targetStep = 0; - if (m_Parameters.AppendAfterSteps + static_cast(availableSteps) < 0) - { - targetStep = 0; - } - else if (m_Parameters.AppendAfterSteps < 0) + + if (m_Parameters.AppendAfterSteps < 0) { - // -1 means last step - targetStep = availableSteps + m_Parameters.AppendAfterSteps + 1; + // -1 means append after last step + int s = (int)availableSteps + m_Parameters.AppendAfterSteps + 1; + if (s < 0) + { + s = 0; + } + targetStep = static_cast(s); } else {