From c832f6843ef517f94a83bb46e8152f18aeda89a3 Mon Sep 17 00:00:00 2001 From: Jayghosh Rao Date: Fri, 18 Jun 2021 01:02:00 +0200 Subject: [PATCH] Fix _nThreads lower bound to 0 (#83) Fix problem with negative NTHREADS values When a negative value is passed to NTHREADS, the value is read as int and afterwards assigned to an unsigned int. During the assignment, the negative value wraps around and becomes a ridiculously high number. This can cause problems with TBB due to std::bad_alloc. Now, the value is clipped to the nonnegative range before it is further used. --- src/libcadet/SimulatorImpl.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libcadet/SimulatorImpl.cpp b/src/libcadet/SimulatorImpl.cpp index 187bb1583..b939a7168 100644 --- a/src/libcadet/SimulatorImpl.cpp +++ b/src/libcadet/SimulatorImpl.cpp @@ -1473,7 +1473,11 @@ namespace cadet paramProvider.popScope(); if (paramProvider.exists("NTHREADS")) - _nThreads = paramProvider.getInt("NTHREADS"); + { + // Ensure numThreads >= 0 + const int numThreads = paramProvider.getInt("NTHREADS"); + _nThreads = std::max(numThreads, 0); + } else _nThreads = 0;