https://bugs.kde.org/show_bug.cgi?id=528747

            Bug ID: 528747
           Summary: qcolor-from-literal crashes (SIGABRT) on
                    QColor::fromString(QStringLiteral(...)) with LLVM 21
    Classification: Developer tools
           Product: clazy
      Version First unspecified
       Reported In:
          Platform: Kubuntu
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: general
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
  Target Milestone: ---

The qcolor-from-literal check aborts clazy-standalone with an assertion
failure when it sees QColor::fromString(QStringLiteral("#...")).

Versions
--------
  clazy-standalone --version
    Ubuntu LLVM version 21.1.8
    Optimized build.
    clazy version 1.17

  clazy 1.17 (Ubuntu package clazy 1.17-2; links libclang-cpp.so.21.1,
  libLLVM.so.21.1)
  Qt 6.10.2, gcc 15.2, -std=c++17

I also checked clazy master (2026-11-12): both getString() calls in
src/checks/level0/qcolor-from-literal.cpp are still
fixed upstream yet. I searched bugzilla for "clazy g
assert", "clazy crash" and the open bug list; I foun
qcolor-from-literal, LLVM 21 or this assertion.

Minimal reproduction
--------------------
min.cpp:

    #include <QColor>
    #include <QString>

    int main() {
        QColor a = QColor::fromString(QStringLiteral
        return a.isValid() ? 0 : 1;
    }

compile_commands.json:

    [{
      "directory": "/tmp/repro",
      "file": "/tmp/repro/min.cpp",
      "command": "c++ -std=c++17 \
        -isystem /usr/include/x86_64-linux-gnu/qt6 \
        -isystem /usr/include/x86_64-linux-gnu/qt6/QtCore \
        -isystem /usr/include/x86_64-linux-gnu/qt6/Q
        -fPIC -c /tmp/repro/min.cpp -o min.o"
    }]

Run:
    clazy-standalone -p /tmp/repro /tmp/repro/min.cpp

Result: exit 134 (SIGABRT).

    clazy-standalone: /usr/lib/llvm-21/include/clang
    llvm::StringRef clang::StringLiteral::getString(
    Assertion `(isUnevaluated() || getCharByteWidth(
    is used in places that assume strings use char"'

What triggers it
----------------
  QColor::fromString(QStringLiteral("#1a1a1a"))   -> crash (134)
  QColor::fromString(QString("#1a1a1a"))          ->
  QColor(QStringLiteral("#1a1a1a"))               ->

Only the fromString matcher combined with a QStringL
With --checks=qcolor-from-literal the minimal file crashes; with
--checks="level0,level1,no-qcolor-from-literal" it does not (rc=0), so this
check is the sole cause.

Backtrace (from the core dump)
------------------------------
  #7  __assert_fail (assertion="(isUnevaluated() || ..",
                     file="/usr/lib/llvm-21/include/clang/AST/Expr.h")
  #8  ?? () from ClazyPlugin.so +0x4776b
  #9  ?? () from ClazyPlugin.so +0x519d1
  #10 ?? () from libclang-cpp.so.21.1
  #11 clang::ast_matchers::internal::BoundNodesTreeB
  ...
  #22 clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&)
  #23 clang::ParseAST(...)

Where it comes from
-------------------
src/checks/level0/qcolor-from-literal.cpp has two ca
not check the literal's character width first:

  line  76:  llvm::StringRef str = lt->getString();
  line 165:  if (lt && isStringColorLiteralPattern(l

The path reaching line 76 builds the literal from th

  if (auto res = result.Nodes.getNodeAs<CallExpr>("m
      if (Expr *argExpr = const_cast<Expr *>(res->getArg(0))) {
          lt = clazy::getFirstChildOfType<StringLite

Under Qt 6, QStringLiteral expands to QT_UNICODE_LIT
a char16_t literal whose getCharByteWidth() is 2. LL
StringLiteral::getString() requires a char (or uneva
LLVM 21 enforces that with an assertion.

Impact
------
qcolor-from-literal is in level0, so it is enabled by default. Any Qt project
containing QColor::fromString(QStringLiteral(...)) g
translation unit, and the whole TU receives no analy
this check. In our tree this silently hid a test fil
until we added exit-status checking to our lint gate

Related
-------
This looks like the same class as bug 527051 ("qproperty-type-mismatch
segfaults when Q_PROPERTY is expanded from inside an
consuming an AST node produced by macro expansion wi
shape it actually gets.

---

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to