Repository navigation
fix: escape percent signs in journal message handler to prevent crash - #302
Conversation
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
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Reviewer's GuideImplementationEscape percent signs in journal messages before passing the MESSAGE field to the printf-style journald API to prevent format-string crashes.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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>| // 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("%", "%%"); |
There was a problem hiding this comment.
🟡 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`.| // 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("%", "%%"); |
There was a problem hiding this comment.
⚪ 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 `%`.| // 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; |
There was a problem hiding this comment.
⚪ 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`)|
/forcemerge |
|
This pr force merged! (status: unstable) |
e49d71e
into
linuxdeepin:develop/snipe
Root Cause Analysis
journalMessageHandlerinsrc/index/main.cpppasses the raw Qt log message as theMESSAGE=field tosd_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.bmpoutput bycontrollerpipe.cpp:196— they are interpreted as conversion specifiers, causing out-of-bounds reads and a SEGV. The coredump stack (frame #4sd_journal_send_with_location→ SEGV) and the minimal demo (crash_demoEXIT_CODE=139) confirm this mechanism.Fix
Escape every
%as%%in the message before building themsgField, so the literal character is preserved and no spurious format conversion is triggered. The fix is a single-point change insidejournalMessageHandler(src/index/main.cpp), consistent with the analysis report's recommended root-cause-layer fix.Change Safety Assessment
Code Safety
QByteArray::replace("%", "%%")call on a local variable before constructingmsgField; no function signature change, no public API impact, single caller (qInstallMessageHandleratmain.cpp:109) unaffected.journalMessageHandlerwas introduced by commit08f68770(trust client integration); the modified line is not a prior bug fix, so no historical fix is reverted.Business Impact Scope
deepin-anything-index): when indexing files whose paths contain%characters (URL-encoded names), the service no longer crashes during journal logging.%are unaffected; journal output shows%%where%appeared (cosmetic only).Verification Suggestion
%(e.g.%E7%9F%B3%E9%BC%93%E5%AF%BA.bmp) and confirmdeepin-anything-indexdoes not crash.根因分析
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 堆栈(帧 #4sd_journal_send_with_location→ SEGV)与最小复现 demo(crash_demoEXIT_CODE=139)确认此机理。修复方案
构建
msgField前将消息中每个%转义为%%,保留字面字符,不触发格式转换。改动为journalMessageHandler内单点修改(src/index/main.cpp),与分析报告推荐的根因层修复方案一致。改动安全评估
代码安全评估
QByteArray::replace("%", "%%"),不改变函数签名、不影响公开 API,唯一调用者(main.cpp:109的qInstallMessageHandler)不受影响。journalMessageHandler由 commit08f68770(trust client 集成)引入,被修改行非历史 bug 修复产物,不撤销任何已有修复。业务影响范围
deepin-anything-index):索引含%字符路径的文件时,服务不再因日志记录崩溃。%的消息不受影响;journal 输出中%显示为%%(仅显示层面)。验证建议
%E7%9F%B3%E9%BC%93%E5%AF%BA.bmp),确认deepin-anything-index不崩溃。Summary by Sourcery
Bug Fixes: