Repository navigation
Description
Feature description
qApp and qGuiApp are not plain accessors. They are macros, and what they expand to depends on which application header a translation unit sees:
| Included | qApp expands to |
|---|---|
<QCoreApplication> only |
QCoreApplication::instance() |
<QGuiApplication> |
static_cast<QGuiApplication *>(QCoreApplication::instance()) |
<QApplication> |
static_cast<QApplication *>(QCoreApplication::instance()) |
qGuiApp is always static_cast<QGuiApplication *>(QCoreApplication::instance()).
In a process whose application object is only a QCoreApplication, each of these static_casts is a downcast to a type the object doesn't have, which is undefined behaviour, even when the result is only passed to qobject_cast. Calling a QGuiApplication member through it, as qGuiApp->palette() does, isn't safe either.
src/libsync, src/resources and src/common are such code: they run in the GUI-less test binaries (QTEST_GUILESS_MAIN, a plain QCoreApplication), and libsync's aboutVersions() says it "may be called by both GUI and CLI". The headers a file sees aren't always its own: in the tests, <QtTest> includes <QApplication> because they link Qt Widgets through owncloudGui.
This came up in #12722, which adds a Linux test job with AddressSanitizer and UBSan: UBSan reported downcast of address … which does not point to an object of type 'QGuiApplication' at src/libsync/theme.cpp:170 (qobject_cast<QGuiApplication *>(qApp)), reached by every test through setUpTests() → Logger::setLogFile() → Theme::aboutVersions(), and 24 of 37 tests failed on it. #12722 fixes that call and the same pattern in platform_win.cpp. With halt_on_error, each test stopped at its first report; once that one was fixed, UBSan also reported qGuiApp->styleHints() at src/resources/iconresources.cpp:45 in ProtocolModelTest, which #12722 fixes too, together with the other qGuiApp uses in src/resources. With those, the suite passes under UBSan; the uses still open below are those the tests don't reach as a downcast today.
Uses on master:
| File | Use | Sees | Risk |
|---|---|---|---|
src/libsync/theme.cpp:170 |
qobject_cast<QGuiApplication *>(qApp) |
QGuiApplication |
downcast; reported by UBSan, fixed in #12722 |
src/libsync/platform_win.cpp:103 |
qobject_cast<QGuiApplication *>(qApp) |
QApplication |
downcast; fixed in #12722 |
src/libsync/platform_win.cpp:118 |
QMetaObject::invokeMethod(qApp, &QApplication::quit) |
QApplication |
downcast; fixed in #12722 |
src/resources/iconresources.cpp:45 |
qGuiApp->styleHints()->colorScheme() |
QGuiApplication |
downcast, only to call the static styleHints(); reported by UBSan, fixed in #12722 |
src/resources/iconresources.cpp:250 |
qGuiApp->palette() (buildAvatar(), reached from Account::avatar() in libsync) |
QGuiApplication |
downcast, only to call the static palette(); fixed in #12722 |
src/resources/resources.cpp:51 |
qGuiApp->styleHints() (IconCache constructor) |
QGuiApplication |
downcast, only to call the static styleHints(); fixed in #12722 |
src/resources/themewatcher.cpp:25 |
qApp->installEventFilter(this) |
QCoreApplication |
none today; depends on the includes |
src/libsync/logger.cpp:148, 244, 286, 296, 320 |
qApp->applicationName() |
QCoreApplication |
none today; depends on the includes |
src/common/utility.cpp:115 |
qApp->applicationName() |
QCoreApplication |
none today; depends on the includes |
src/common/utility_unix.cpp:95 |
qApp->applicationDirPath() |
QCoreApplication |
none today; depends on the includes |
The ones marked "none today" are correct only because those files happen to include <QCoreApplication> alone. One more include, directly or through a header, turns them into the same downcast, with no compiler warning.
Proposed solution
- Replace the macros in
src/libsync,src/resourcesandsrc/common:- static members through the class:
QCoreApplication::applicationName(),QCoreApplication::applicationDirPath(); - the instance through
QCoreApplication::instance(), e.g.QCoreApplication::instance()->installEventFilter(this), andqobject_cast<QGuiApplication *>(QCoreApplication::instance())where GUI-only information is optional; - the
qGuiAppuses insrc/resourcesturned out to call only static members (styleHints(),palette()), so calling them through the class is enough; ci(linux): run the tests with AddressSanitizer and UBSan too #12722 does that.
- static members through the class:
- Reject the macros in CI for these directories, so they don't come back, e.g. a step that fails on
(comments included, which is simpler than telling them apart and costs nothing). It could run in an existing lint job; a clang-tidy or clazy check would also work, but a grep needs no build.
git grep -nwE 'qApp|qGuiApp' -- src/libsync src/resources src/common
src/gui is out of scope: it always runs with a QApplication, so qApp is correct there.
Alternative solutions you considered
- Fix only what UBSan reports, as ci(linux): run the tests with AddressSanitizer and UBSan too #12722 does: it covers the paths the tests reach, not those they don't, and a new
qAppin these directories goes unnoticed until a sanitizer job happens to reach it. #undef qApp/ redefine it in a common header for these directories: it changes the meaning of a well-known Qt macro behind the reader's back and depends on include order; explicit calls are clearer.
Additional context
qAppin Qt 6.10.3:src/gui/kernel/qguiapplication.h:29,#define qApp (static_cast<QGuiApplication *>(QCoreApplication::instance()));qapplication.handqcoreapplication.hdefine it likewise for their classes.- The UBSan report from ci(linux): run the tests with AddressSanitizer and UBSan too #12722's first run (job 139238783256):
src/libsync/theme.cpp:170:55: runtime error: downcast of address 0x7fd0e4d0d730 which does not point to an object of type 'QGuiApplication' 0x7fd0e4d0d730: note: object is of type 'QCoreApplication' #0 OCC::Theme::aboutVersions(OCC::Theme::VersionFormat) const src/libsync/theme.cpp:170 #1 OCC::Logger::open(QString const&) src/libsync/logger.cpp:148 #2 OCC::Logger::setLogFile(QString const&) src/libsync/logger.cpp:173 #3 setUpTests test/testutils/testutilsloader.cpp:18 - Related, closed: Invokes on qApp #10835 (invokes on
qApp).
🤖 Generated with Claude Code
Activity