Skip to content

fix: escape percent signs in journal message handler to prevent crash - #302

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:agent/bugfix/384803
Nov 30, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:agent/bugfix/384803

Conversation

@Johnson-zs

@Johnson-zs Johnson-zs commented Nov 30, 2026 •

Copy link
Copy Markdown

Root Cause Analysis

journalMessageHandler in src/index/main.cpp passes the raw Qt log message as the MESSAGE= field to sd_journal_send_with_location, whose 4th parameter is a printf-style format string. When the message contains bare % characters — e.g. from URL-encoded file paths like %E7%9F%B3%E9%BC%93%E5%AF%BA.bmp output by controllerpipe.cpp:196 — they are interpreted as conversion specifiers, causing out-of-bounds reads and a SEGV. The coredump stack (frame #4 sd_journal_send_with_location → SEGV) and the minimal demo (crash_demo EXIT_CODE=139) confirm this mechanism.

Fix

Escape every % as %% in the message before building the msgField, so the literal character is preserved and no spurious format conversion is triggered. The fix is a single-point change inside journalMessageHandler (src/index/main.cpp), consistent with the analysis report's recommended root-cause-layer fix.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • The change adds a QByteArray::replace("%", "%%") call on a local variable before constructing msgField; no function signature change, no public API impact, single caller (qInstallMessageHandler at main.cpp:109) unaffected.
  • journalMessageHandler was introduced by commit 08f68770 (trust client integration); the modified line is not a prior bug fix, so no historical fix is reverted.

Business Impact Scope

  • File indexing service (deepin-anything-index): when indexing files whose paths contain % characters (URL-encoded names), the service no longer crashes during journal logging.
  • Normal logging: messages without % are unaffected; journal output shows %% where % appeared (cosmetic only).

Verification Suggestion

  • Index a file with a URL-encoded name containing % (e.g. %E7%9F%B3%E9%BC%93%E5%AF%BA.bmp) and confirm deepin-anything-index does not crash.
  • Test Ctrl+C interrupt during indexing to verify graceful exit.
  • Verify normal file indexing and journal logging show no regression.

根因分析

src/index/main.cpp 的 journalMessageHandler 将原始 Qt 日志消息作为 MESSAGE= 字段传给 sd_journal_send_with_location,其第 4 参数是 printf 格式串。当消息含裸 % 字符时(如 controllerpipe.cpp:196 输出的 URL 编码文件路径 %E7%9F%B3%E9%BC%93%E5%AF%BA.bmp),% 被当作格式转换符,导致越界读取和 SEGV。coredump 堆栈(帧 #4 sd_journal_send_with_location → SEGV)与最小复现 demo(crash_demo EXIT_CODE=139)确认此机理。

修复方案

构建 msgField 前将消息中每个 % 转义为 %%,保留字面字符,不触发格式转换。改动为 journalMessageHandler 内单点修改(src/index/main.cpp),与分析报告推荐的根因层修复方案一致。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 改动仅在局部变量上添加 QByteArray::replace("%", "%%"),不改变函数签名、不影响公开 API,唯一调用者(main.cpp:109 的 qInstallMessageHandler)不受影响。
  • journalMessageHandler 由 commit 08f68770(trust client 集成)引入,被修改行非历史 bug 修复产物,不撤销任何已有修复。

业务影响范围

  • 文件索引服务(deepin-anything-index):索引含 % 字符路径的文件时,服务不再因日志记录崩溃。
  • 正常日志:不含 % 的消息不受影响;journal 输出中 % 显示为 %%(仅显示层面)。

验证建议

  • 索引含 URL 编码百分号文件名的文件(如 %E7%9F%B3%E9%BC%93%E5%AF%BA.bmp),确认 deepin-anything-index 不崩溃。
  • 测试索引过程中 Ctrl+C 中断是否正常退出。
  • 验证普通文件索引和日志记录无回归。

Summary by Sourcery

Bug Fixes:

  • Prevent the journal message handler from crashing when log messages contain percent signs, including URL-encoded file paths.

1. Root cause: journalMessageHandler in src/index/main.cpp passes the
   raw Qt message as the MESSAGE field to sd_journal_send_with_location,
   whose 4th parameter is a printf-style format string; bare '%' in
   URL-encoded file paths (e.g. %E7%9F%B3%E9%BC%93%E5%AF%BA.bmp) are
   interpreted as conversion specifiers, causing out-of-bounds reads
   and a SEGV
2. Fix: escape every '%' as '%%' in the message before building the
   msgField, so the literal character is preserved and no spurious
   format conversion is triggered
3. Impact: only affects the journal logging path in
   deepin-anything-index; no API changes, no behavior change for
   messages without '%' characters

fix: 修复日志消息处理器中百分号未转义导致的崩溃

1. 根因:src/index/main.cpp 的 journalMessageHandler 将原始 Qt 消息
   作为 MESSAGE 字段传给 sd_journal_send_with_location,其第 4 参数
   是 printf 格式串;URL 编码文件路径中的裸 '%'(如
   %E7%9F%B3%E9%BC%93%E5%AF%BA.bmp)被当作格式转换符,导致越界读取
   和 SEGV
2. 方案:构建 msgField 前将消息中每个 '%' 转义为 '%%',保留字面
   字符,不触发格式转换
3. 影响:仅影响 deepin-anything-index 的日志记录路径;无 API 变更,
   不含 '%' 的消息无行为变化

Log: Fixed crash in deepin-anything-index when logging file paths containing percent signs
Influence:
  - Test indexing files with URL-encoded names containing % characters
  - Test Ctrl+C interrupt during indexing to verify no crash
  - Verify normal file indexing and journal logging show no regression
Log: 修复 deepin-anything-index 索引含百分号文件名时日志记录崩溃的问题
Influence:
  - 测试索引含 URL 编码百分号字符的文件名
  - 测试索引过程中 Ctrl+C 中断是否正常退出
  - 验证普通文件索引和日志记录无回归
PMS: BUG-384803
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Escape percent signs in journal messages before passing the MESSAGE field to the printf-style journald API to prevent format-string crashes.

  • src/index/main.cpp L62-70

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Johnson-zs
Johnson-zs marked this pull request as draft November 30, 2026 05:45

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/index/main.cpp" line_range="69" />
<code_context>
+    // a crash when it tries to read non-existent variadic arguments. Escape
+    // every '%' as '%%' so the literal character is preserved.
+    QByteArray escapedMessage = message;
+    escapedMessage.replace("%", "%%");
+    const QByteArray msgField = QByteArrayLiteral("MESSAGE=") + escapedMessage;
     char prioField[32];
</code_context>
<issue_to_address>
**Journal source metadata gets corrupted**

When the source path or Qt function metadata contains a printf conversion, `fileField` and `funcField` are passed unchanged to `sd_journal_send_with_location`, which interprets them as printf formats; conversions consume or misinterpret subsequent arguments, corrupting journal entries or crashing logging.

Escape percent signs in both `fileField` and `funcField` before passing them to `sd_journal_send_with_location`.
</issue_to_address>

### Comment 2
<location path="src/index/main.cpp" line_range="69" />
<code_context>
+    // a crash when it tries to read non-existent variadic arguments. Escape
+    // every '%' as '%%' so the literal character is preserved.
+    QByteArray escapedMessage = message;
+    escapedMessage.replace("%", "%%");
+    const QByteArray msgField = QByteArrayLiteral("MESSAGE=") + escapedMessage;
     char prioField[32];
</code_context>
<issue_to_address>
**Journal output retains one percent**

`sd_journal_send_with_location` processes `%%` as a single literal `%`, so journal entries retain the original percent rather than displaying `%%` as the description claims.

Update the stated impact to say that journal output preserves a single literal `%`.
</issue_to_address>

### Comment 3
<location path="src/index/main.cpp" line_range="68" />
<code_context>
+    // paths like %E7%9F%B3) is interpreted as a conversion specifier, causing
+    // a crash when it tries to read non-existent variadic arguments. Escape
+    // every '%' as '%%' so the literal character is preserved.
+    QByteArray escapedMessage = message;
+    escapedMessage.replace("%", "%%");
+    const QByteArray msgField = QByteArrayLiteral("MESSAGE=") + escapedMessage;
</code_context>
<issue_to_address>
**New variable breaks naming convention**

When the new local variable is named `escapedMessage`, `escapedMessage` uses camelCase instead of the required snake_case, so the changed code violates the repository's variable-naming rule.

Rename it to `escaped_message`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/index/main.cpp
// a crash when it tries to read non-existent variadic arguments. Escape
// every '%' as '%%' so the literal character is preserved.
QByteArray escapedMessage = message;
escapedMessage.replace("%", "%%");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Journal source metadata gets corrupted

When the source path or Qt function metadata contains a printf conversion, fileField and funcField are passed unchanged to sd_journal_send_with_location, which interprets them as printf formats; conversions consume or misinterpret subsequent arguments, corrupting journal entries or crashing logging.

Escape percent signs in both fileField and funcField before passing them to sd_journal_send_with_location.

Prompt for AI agents
In `src/index/main.cpp` at line 69:

**Journal source metadata gets corrupted**

When the source path or Qt function metadata contains a printf conversion, `fileField` and `funcField` are passed unchanged to `sd_journal_send_with_location`, which interprets them as printf formats; conversions consume or misinterpret subsequent arguments, corrupting journal entries or crashing logging.

Escape percent signs in both `fileField` and `funcField` before passing them to `sd_journal_send_with_location`.

Comment thread src/index/main.cpp
// a crash when it tries to read non-existent variadic arguments. Escape
// every '%' as '%%' so the literal character is preserved.
QByteArray escapedMessage = message;
escapedMessage.replace("%", "%%");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · Journal output retains one percent

sd_journal_send_with_location processes %% as a single literal %, so journal entries retain the original percent rather than displaying %% as the description claims.

Update the stated impact to say that journal output preserves a single literal %.

Prompt for AI agents
In `src/index/main.cpp` at line 69:

**Journal output retains one percent**

`sd_journal_send_with_location` processes `%%` as a single literal `%`, so journal entries retain the original percent rather than displaying `%%` as the description claims.

Update the stated impact to say that journal output preserves a single literal `%`.

Comment thread src/index/main.cpp
// paths like %E7%9F%B3) is interpreted as a conversion specifier, causing
// a crash when it tries to read non-existent variadic arguments. Escape
// every '%' as '%%' so the literal character is preserved.
QByteArray escapedMessage = message;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · New variable breaks naming convention

When the new local variable is named escapedMessage, escapedMessage uses camelCase instead of the required snake_case, so the changed code violates the repository's variable-naming rule.

Rename it to escaped_message.

Prompt for AI agents
In `src/index/main.cpp` at line 68:

**New variable breaks naming convention**

When the new local variable is named `escapedMessage`, `escapedMessage` uses camelCase instead of the required snake_case, so the changed code violates the repository's variable-naming rule.

Rename it to `escaped_message`.

From AGENTS.md L59:

- Variables: `snake_case` (e.g., `config_ptr`, `event_handler`)

@Johnson-zs
Johnson-zs marked this pull request as ready for review November 30, 2026 06:16
@Johnson-zs

Copy link
Copy Markdown
Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit e49d71e into linuxdeepin:develop/snipe Nov 30, 2026
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants