Skip to content

fix: fix infinite loop and OOB read in text processing - #454

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/snipefrom
liyigang1:agent/bugfix/383499
Nov 23, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:release/snipefrom
liyigang1:agent/bugfix/383499

Conversation

@liyigang1

@liyigang1 liyigang1 commented Nov 19, 2026 •

Copy link
Copy Markdown

fix: fix infinite loop and OOB read in text processing

  1. Root cause: SecurityDialog::autoCutText() decrements i after
    clearing str, but when a single char exceeds label width str
    is empty after remove, so --i cancels i++ causing infinite loop
  2. Fix: check str.isEmpty() after remove, if empty output the char
    as-is without decrementing i, preventing the loop deadlock
  3. Impact: security dialog text display works for wide chars, no
    behavior change for normal text wrapping
  4. Root cause: PDFPage::words() loop exits on index==charCount but
    charCount may exceed texts/rects container sizes, causing OOB
  5. Fix: add container size checks to loop exit condition and guard
    rects[0] access with isEmpty() check, preventing OOB read
  6. Impact: PDF text extraction handles mismatched container sizes
    safely, no change for normal PDF documents

Log: 修复安全对话框文本裁剪死循环和PDF文本提取越界读取问题

Influence:

  1. Test PDF security dialog text display with normal text
  2. Test security dialog with single wide character exceeding label
  3. Test PDF text selection on normal documents
  4. Test PDF text selection on documents with mismatched char counts

fix: 修复文本处理中的死循环和越界读取问题

  1. 根因:SecurityDialog::autoCutText() 在 str 清空后执行 --i,当
    单字符宽度超过 label 时 str 为空,--i 抵消 i++ 导致死循环
  2. 方案:remove 后检查 str 是否为空,为空时直接输出当前字符不减 i,
    消除循环死锁
  3. 影响:安全对话框文本显示支持超宽字符,正常文本折行无行为变化
  4. 根因:PDFPage::words() 循环以 index==charCount 为退出条件,但
    charCount 可能大于 texts/rects 容器大小导致越界读取
  5. 方案:循环退出条件增加容器大小检查,rects[0] 访问前增加空检查,
    防止越界读取
  6. 影响:PDF 文本提取安全处理容器大小不一致,正常 PDF 文档无变化

Log: 修复安全对话框文本裁剪死循环和PDF文本提取越界读取问题

Influence:

  1. 测试 PDF 安全对话框正常文本显示
  2. 测试安全对话框单宽字符超过 label 宽度场景
  3. 测试正常 PDF 文档文本选择功能
  4. 测试字符数与容器不匹配的 PDF 文档文本选择

PMS: BUG-383499

Summary by Sourcery

Harden text processing against infinite loops and out-of-bounds reads while preserving normal wrapping and PDF extraction behavior.

Bug Fixes:

  • Prevent security dialog text wrapping from hanging when an individual character is wider than the label.
  • Prevent PDF text extraction from reading beyond available text and rectangle data when character counts are inconsistent.

Tests:

  • Add regression coverage for oversized and mismatched PDF text containers, empty extraction results, and wide-character wrapping termination.

@liyigang1
liyigang1 marked this pull request as draft November 19, 2026 13:39
@sourcery-ai

sourcery-ai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Bound PDF word extraction by the available text and rectangle entries, and avoid the empty-rectangle access that caused out-of-bounds reads.

  • reader/document/PDFModel.cpp L186, L200

Ensure autoCutText emits an over-wide character without decrementing the loop index, so wrapping continues instead of hanging.

  • reader/widgets/SecurityDialog.cpp L2, L43-53

Tests

Add regression tests for PDF extraction with oversized character counts, shorter or empty containers, and wrapping when a glyph exceeds the label width.

  • tests/document/ut_pdfmodel.cpp L163-185, L331-373
  • tests/widgets/ut_securitydialog.cpp L14, L102-125

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

@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 2 issues

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

## Individual Comments

### Comment 1
<location path="reader/widgets/SecurityDialog.cpp" line_range="50-51" />
<code_context>
+                    --i;
+                } else {
+                    // 单字符宽度即超过label宽度,单独作为一行输出
+                    newstr.strList.append(QString(strText.at(i)));
+                    newstr.resultStr += strText.at(i) + QString("\n");
+                    str.clear();
+                }
</code_context>
<issue_to_address>
**Supplementary characters display incorrectly**

When a supplementary Unicode character is encountered at a wrapping boundary or is wider than the label, `autoCutText()` processes `strText.at(i)` one UTF-16 code unit at a time, splitting the surrogate pair and causing the dialog to display corrupted or missing text.

Iterate over complete Unicode characters and keep each character’s surrogate pair together when wrapping and appending text.
</issue_to_address>

### Comment 2
<location path="reader/widgets/SecurityDialog.cpp" line_range="50-51" />
<code_context>
+                    --i;
+                } else {
+                    // 单字符宽度即超过label宽度,单独作为一行输出
+                    newstr.strList.append(QString(strText.at(i)));
+                    newstr.resultStr += strText.at(i) + QString("\n");
+                    str.clear();
+                }
</code_context>
<issue_to_address>
**Label reserves an extra line**

When the final character is wider than the label, the oversized-character branch appends the character with a newline and leaves `str` empty, then the unconditional final append adds an empty `strList` entry; `autoFeed()` counts that entry when setting the label height, leaving blank space below the text.

Skip the final `strList` append when `str` is empty.
</issue_to_address>

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

Comment thread reader/widgets/SecurityDialog.cpp Outdated
Comment on lines +50 to +51
newstr.strList.append(QString(strText.at(i)));
newstr.resultStr += strText.at(i) + QString("\n");

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 · Supplementary characters display incorrectly

When a supplementary Unicode character is encountered at a wrapping boundary or is wider than the label, autoCutText() processes strText.at(i) one UTF-16 code unit at a time, splitting the surrogate pair and causing the dialog to display corrupted or missing text.

Iterate over complete Unicode characters and keep each character’s surrogate pair together when wrapping and appending text.

Prompt for AI agents
In `reader/widgets/SecurityDialog.cpp` at lines 50-51:

**Supplementary characters display incorrectly**

When a supplementary Unicode character is encountered at a wrapping boundary or is wider than the label, `autoCutText()` processes `strText.at(i)` one UTF-16 code unit at a time, splitting the surrogate pair and causing the dialog to display corrupted or missing text.

Iterate over complete Unicode characters and keep each character’s surrogate pair together when wrapping and appending text.

Comment thread reader/widgets/SecurityDialog.cpp Outdated
Comment on lines +50 to +51
newstr.strList.append(QString(strText.at(i)));
newstr.resultStr += strText.at(i) + QString("\n");

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 · Label reserves an extra line

When the final character is wider than the label, the oversized-character branch appends the character with a newline and leaves str empty, then the unconditional final append adds an empty strList entry; autoFeed() counts that entry when setting the label height, leaving blank space below the text.

Skip the final strList append when str is empty.

Prompt for AI agents
In `reader/widgets/SecurityDialog.cpp` at lines 50-51:

**Label reserves an extra line**

When the final character is wider than the label, the oversized-character branch appends the character with a newline and leaves `str` empty, then the unconditional final append adds an empty `strList` entry; `autoFeed()` counts that entry when setting the label height, leaving blank space below the text.

Skip the final `strList` append when `str` is empty.

1. Root cause: SecurityDialog::autoCutText() decrements i after
   clearing str, but when a single char exceeds label width str
   is empty after remove, so --i cancels i++ causing infinite loop
2. Fix: check str.isEmpty() after remove, if empty output the char
   as-is without decrementing i, preventing the loop deadlock
3. Impact: security dialog text display works for wide chars, no
   behavior change for normal text wrapping
4. Root cause: PDFPage::words() loop exits on index==charCount but
   charCount may exceed texts/rects container sizes, causing OOB
5. Fix: add container size checks to loop exit condition and guard
   rects[0] access with isEmpty() check, preventing OOB read
6. Impact: PDF text extraction handles mismatched container sizes
   safely, no change for normal PDF documents

Log: 修复安全对话框文本裁剪死循环和PDF文本提取越界读取问题

Influence:
1. Test PDF security dialog text display with normal text
2. Test security dialog with single wide character exceeding label
3. Test PDF text selection on normal documents
4. Test PDF text selection on documents with mismatched char counts

fix: 修复文本处理中的死循环和越界读取问题

1. 根因:SecurityDialog::autoCutText() 在 str 清空后执行 --i,当
   单字符宽度超过 label 时 str 为空,--i 抵消 i++ 导致死循环
2. 方案:remove 后检查 str 是否为空,为空时直接输出当前字符不减 i,
   消除循环死锁
3. 影响:安全对话框文本显示支持超宽字符,正常文本折行无行为变化
4. 根因:PDFPage::words() 循环以 index==charCount 为退出条件,但
   charCount 可能大于 texts/rects 容器大小导致越界读取
5. 方案:循环退出条件增加容器大小检查,rects[0] 访问前增加空检查,
   防止越界读取
6. 影响:PDF 文本提取安全处理容器大小不一致,正常 PDF 文档无变化

Log: 修复安全对话框文本裁剪死循环和PDF文本提取越界读取问题

Influence:
1. 测试 PDF 安全对话框正常文本显示
2. 测试安全对话框单宽字符超过 label 宽度场景
3. 测试正常 PDF 文档文本选择功能
4. 测试字符数与容器不匹配的 PDF 文档文本选择

PMS: BUG-383499
@liyigang1
liyigang1 marked this pull request as ready for review November 23, 2026 05:35
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: liyigang1, lzwind

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

@liyigang1

Copy link
Copy Markdown
Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit b99ed8c into linuxdeepin:release/snipe Nov 23, 2026
8 of 9 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.

3 participants