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

QColor initialization via color name string susceptible to static initialization order fiasco

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • P2: Important
    • None
    • 5.10.0
    • GUI: Painting
    • None
    • (1) Ubuntu Linux 16.04/GCC; (2) Windows 10/Microsoft Visual Studio 2015; (3) Windows 10/Microsoft Visual Studio 2017. All with custom compiled versions of Qt 5.10.0 (compiled with the same compiler as the user code).

    Description

      If a static QColor is constructed using a colour name, this can fail unpredictably by giving an invalid QColor (e.g. in test code as below: works under Linux/GCC, fails under Windows/MSVC2015 or MSVC2017).

      const QColor THE_COLOUR("purple");  // may work, may fail
      const QColor SAME_COLOUR(128, 0, 128);  // always works
      void somefunc_called_after_main()
      {
          const QColor ANOTHER_COLOUR("purple");  // always fine
      }
      

      In qcolor.cpp, the colour-name initializer ultimately comes through to get_named_rgb_no_space() which uses this private static variable in qcolor.cpp:

      static const struct RGBData {
          const char name[21];
          uint value;
      } rgbTbl[] = {
          { "aliceblue", rgb(240, 248, 255) },
          // ...
      };
      

      Therefore, I presume that the problem is that it's undefined by the C++ standard whether rgbTbl in qcolor.cpp has been initialized by the time that user code initializes its module-level variables. If it hasn't, colour-name-based initialization will fail.

      This seems to be undocumented and can lead to unpredictable failures (with success/silent failure being related to the specific compiler in use and/or luck). Whilst there are obvious workarounds (e.g. just using RGB numbers), would it be possible to have qcolor.cpp initialize its static rgbTbl when first requested (e.g. as per https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use)?

       

      Attachments

        1. main.cpp
          2 kB
        2. other.cpp
          0.1 kB
        3. other.h
          0.1 kB
        4. qcolor_static_init_fiasco.pro
          0.2 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            rudolf@pobox.com Rudolf Cardinal
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes