diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-12-22 15:06:10 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-01-08 18:29:26 +0100 |
commit | 936e72d18075b79c8d29353618dfbd052ae59dae (patch) | |
tree | 646a4f15bed717648ee990863a6993b8e3c4f351 /tests/auto/concurrent | |
parent | 8ce261f6d857e8b05c8043042fce101cb95cd868 (diff) |
Fix QThreadPool::maxThreadCount() usage
The docs claim that QThreadPool always creates at least one thread.
However, the user can (usually by mistake) request zero or a negative
number of threads.
The maxThreadCount() function is simply returning the value, that was
requested by the user.
Since it's a public API, it is used in several places in QtConcurrent,
where it is assumed that the value is always positive. This can lead
to a crash if the user sets zero as a maxThreadCount.
Update all such places with std::max(maxThreadCount(), 1).
Prefer this approach over changing the return value of
maxThreadCount(), because its behavior is documented and tested.
Amends 885eff053797d56f2e295558d0a71b030fbb1a69.
Fixes: QTBUG-120335
Pick-to: 6.7 6.6 6.5 6.2
Change-Id: Id3b2087cec7fbc7a2d42febca6586f2dacffe444
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/concurrent')
-rw-r--r-- | tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 94248404da..e66edb1b60 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -185,6 +185,17 @@ void tst_QtConcurrentMap::map() QCOMPARE(list, NonTemplateSequence({ 2, 4, 6 })); } + // custom pool with invalid number of threads + { + QList<int> list; + list << 1 << 2 << 3; + QThreadPool pool; + pool.setMaxThreadCount(0); // explicitly set incorrect value + // This should not crash + QtConcurrent::map(&pool, list, MultiplyBy2InPlace()).waitForFinished(); + QCOMPARE(list, QList<int>() << 2 << 4 << 6); + } + #if 0 // not allowed: map() with immutable sequences makes no sense { @@ -1075,6 +1086,17 @@ void tst_QtConcurrentMap::mappedReducedThreadPool() intCube, intSumReduce); QCOMPARE(result, sumOfCubes); } + + { + // pool with invalid number of threads + QThreadPool pool; + pool.setMaxThreadCount(0); // explicitly set incorrect value + + // This should not crash + NonTemplateSequence list { 1, 2, 3 }; + auto future = QtConcurrent::mappedReduced(&pool, list, multiplyBy2, intSumReduce); + QCOMPARE(future.result(), 12); + } } void tst_QtConcurrentMap::mappedReducedWithMoveOnlyCallable() |