OSDN Git Service

validate stack size in QThread::setStackSize()
authorIvailo Monev <xakepa10@laimg.moc>
Sat, 21 Mar 2020 16:13:18 +0000 (16:13 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Sat, 21 Mar 2020 16:13:18 +0000 (16:13 +0000)
that way threads will not fail if it is less than the minimum, the warning
from QThread::start() is basically invisible to GUI users unless
application is started from terminal which is far from ideal. one has
to install own message handler via qInstallMsgHandler() to make such
warnings noticable to GUI users, possible via QErrorMessage, which should
be errors in fact and printed via qCritical() but making such changes is
beyond the scope of this commit

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/core/thread/qthread.cpp

index 6dd0dc5..5090dcf 100644 (file)
@@ -42,6 +42,8 @@
 #include "qcoreapplication_p.h"
 #include "qscopedpointer.h"
 
+#include <limits.h> // for PTHREAD_STACK_MIN
+
 QT_BEGIN_NAMESPACE
 
 /*
@@ -432,6 +434,15 @@ void QThread::setStackSize(uint stackSize)
     QMutexLocker locker(&d->mutex);
     Q_ASSERT_X(!d->running, "QThread::setStackSize",
                "cannot change stack size while the thread is running");
+#ifdef PTHREAD_STACK_MIN
+    int stack_min = sysconf(_SC_THREAD_STACK_MIN);
+    if (stack_min == -1)
+        stack_min = PTHREAD_STACK_MIN;
+    if (Q_UNLIKELY(stackSize < stack_min)) {
+        qWarning("QThread::setStackSize: %u is less than the minimum %u", stackSize, stack_min);
+        stackSize = stack_min;
+    }
+#endif
     d->stackSize = stackSize;
 }