Skip to content

Commit

Permalink
Fix _nThreads lower bound to 0 (#83)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jayghoshter committed Jun 17, 2021
1 parent 62222c7 commit c832f68
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/libcadet/SimulatorImpl.cpp
Expand Up @@ -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;

Expand Down

0 comments on commit c832f68

Please sign in to comment.