sashoalm
sashoalm

Reputation: 79527

Q_ASSERT - "Incorrect format specifier"

I have a Q_ASSERT():

Q_ASSERT(QString("%1").arg(0) != "0");

When it fails, it shows an Abort/Retry/Ignore message box, but in the text it says:

File: f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
Line: 1120
Expression: ("Incorrect format specifier", 0)

With this call stack:

0   _output_s_l output.c    1120    0x5dbee38d  
1   _vsnprintf_helper   vsprintf.c  140 0x5dbb00e8  
2   _vsnprintf_s_l  vsprintf.c  288 0x5dbb0610  
3   _vsnprintf_s    vsprintf.c  340 0x5dbb0880  
4   _VCrtDbgReportA dbgrptt.c   301 0x5dc2571e  
5   _CrtDbgReportV  dbgrpt.c    241 0x5dc24992  
6   _CrtDbgReport   dbgrpt.c    258 0x5dc2494b  
7   qt_message_output   qglobal.cpp 2232    0x5d43bacf  
8   qt_message  qglobal.cpp 2298    0x5d43bc69  
9   qFatal  qglobal.cpp 2481    0x5d43c059  
10  qt_assert   qglobal.cpp 1999    0x5d43b629  

But if I change it to:

Q_ASSERT(QString("0").arg(0) != "0");

It works as expected, showing

File: global\qglobal.cpp
Line: 2232

ASSERT: "QString("0").arg(0) != "0"" in file ..\MyProject\tester.cpp, line 132

So why this strange behavior, is it a bug in Qt?

Upvotes: 0

Views: 2699

Answers (1)

sashoalm
sashoalm

Reputation: 79527

Turned out to be a bug in Qt, in qt_message_output they have this line:

int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf);

Should have been

int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, "%s", buf);

They are passing the entire line contained in Q_ASSERT() as a format specifier.

Upvotes: 1

Related Questions