diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index c369259788d..11a43f73efc 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1263,6 +1263,18 @@ void QMessagePattern::setPattern(const QString &pattern) #if defined(QLOGGING_HAVE_BACKTRACE) && !defined(QT_BOOTSTRAPPED) // make sure the function has "Message" in the name so the function is removed +/* + A typical backtrace in debug mode looks like: + #0 backtraceFramesForLogMessage (frameCount=5) at qlogging.cpp:1296 + #1 formatBacktraceForLogMessage (backtraceParams=..., function=0x4040b8 "virtual void MyClass::myFunction(int)") at qlogging.cpp:1344 + #2 qFormatLogMessage (type=QtDebugMsg, context=..., str=...) at qlogging.cpp:1452 + #3 stderr_message_handler (type=QtDebugMsg, context=..., message=...) at qlogging.cpp:1744 + #4 qDefaultMessageHandler (type=QtDebugMsg, context=..., message=...) at qlogging.cpp:1795 + #5 qt_message_print (msgType=QtDebugMsg, context=..., message=...) at qlogging.cpp:1840 + #6 qt_message_output (msgType=QtDebugMsg, context=..., message=...) at qlogging.cpp:1891 + #7 QDebug::~QDebug (this=, __in_chrg=) at qdebug.h:111 +*/ +static constexpr int TypicalBacktraceFrameCount = 8; #if (defined(Q_CC_GNU) || __has_attribute(optimize)) \ && !defined(Q_CC_INTEL) && !defined(Q_CC_CLANG) @@ -1282,7 +1294,7 @@ static QStringList backtraceFramesForLogMessage(int frameCount) // This code is protected by QMessagePattern::mutex so it is thread safe on all compilers static const QRegularExpression rx(QStringLiteral("^(?:[^(]*/)?([^(/]+)\\(([^+]*)(?:[\\+[a-f0-9x]*)?\\) \\[[a-f0-9x]*\\]$")); - QVarLengthArray buffer(8 + frameCount); + QVarLengthArray buffer(TypicalBacktraceFrameCount + frameCount); int n = backtrace(buffer.data(), buffer.size()); if (n > 0) { int numberPrinted = 0;