Details
-
Bug
-
Resolution: Done
-
P3: Somewhat important
-
5.11.2
-
None
-
Windows 10
-
-
a847f5cd8562d3d42fb4cbdb42367466119f8d5f (qt/qtbase/5.12)
Description
I recently got some errors of kind "<error message> (Operation succeeded)" after calling QTimer::singleShot. After inspecting Qt source code to find out more about the issue, I realized that there was a bug in the qErrnoWarning function: when it calls GetLastError(), it always gets 0 while it should have retrieved a valid system error code; and it happens only if its msg argument is not an empty string.
This is probably caused by QString::vasprintf calling some system function internally, which succeeds and then clears the last error code. An easy fix would be to call qt_error_string (which calls GetLastError) before the call to QString::vasprintf.
Here is some code that demonstrates the problem:
#include <QCoreApplication> #include <QDebug> #include <Windows.h> int main(int argc, char** argv) { QCoreApplication application(argc, argv); OFSTRUCT of; // using system calls OpenFile("invalid_filename", &of, OF_READ); char error1[1024]; FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error1, sizeof(error1), nullptr ); qDebug() << error1; // using qErrnoWarning with empty message OpenFile("invalid_filename", &of, OF_READ); qErrnoWarning(""); // using qErrnoWarning with non-empty message OpenFile("invalid_filename", &of, OF_READ); qErrnoWarning("Error"); return application.exec(); }
Output (translated from french, so may be inaccurate):
The specified file cannot be found. (The specified file cannot be found.) Error (The operation succeeded.)
I'm sorry if this bug is already fixed in more recent versions of Qt, I cannot test with the latest.