Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-77322

qErrnoWarning never prints the actual error message on Windows

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Done
    • Affects Version/s: 5.11.2
    • Fix Version/s: 5.12.5, 5.14.0 Alpha
    • Component/s: Core: Other
    • Labels:
      None
    • Environment:
      Windows 10
    • Platform/s:
      Windows
    • Commits:
      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.

        Attachments

        For Gerrit Dashboard: QTBUG-77322
        # Subject Branch Project Status CR V

          Activity

            People

            Assignee:
            thiago Thiago Macieira
            Reporter:
            laurentgom Laurent
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes