From c0827adfc009ca71430ef5c79b3ddde0979a4aa5 Mon Sep 17 00:00:00 2001 From: "liyigang1@uniontech.com" Date: Thu, 19 Nov 2026 21:36:50 +0800 Subject: [PATCH] fix: fix infinite loop and OOB read in text processing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- reader/document/PDFModel.cpp | 4 +- reader/widgets/SecurityDialog.cpp | 17 +++++--- tests/document/ut_pdfmodel.cpp | 66 +++++++++++++++++++++++++++++ tests/widgets/ut_securitydialog.cpp | 25 +++++++++++ 4 files changed, 105 insertions(+), 7 deletions(-) diff --git a/reader/document/PDFModel.cpp b/reader/document/PDFModel.cpp index cd5595c5e..707037c8e 100644 --- a/reader/document/PDFModel.cpp +++ b/reader/document/PDFModel.cpp @@ -183,7 +183,7 @@ QList PDFPage::words() m_wordLoaded = true; - if (charCount <= 0) { + if (charCount <= 0 || texts.isEmpty() || rects.isEmpty()) { qCDebug(appLog) << "PDFPage::words() - No characters found, returning empty word list"; return m_words; } @@ -197,7 +197,7 @@ QList PDFPage::words() QList rowWords; while (1) { - if (index == charCount) { + if (index >= texts.size() || index >= rects.size() || index == charCount) { // qCDebug(appLog) << "PDFPage::words() - Processed all characters, appending final row"; m_words.append(rowWords); break; diff --git a/reader/widgets/SecurityDialog.cpp b/reader/widgets/SecurityDialog.cpp index 4b062f989..7c9090995 100644 --- a/reader/widgets/SecurityDialog.cpp +++ b/reader/widgets/SecurityDialog.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd. -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2023 -2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -40,10 +40,17 @@ NewStr autoCutText(const QString &text, DLabel *pDesLbl) if (font_label.horizontalAdvance(str) > width) { //根据label宽度调整每行字符数 str.remove(str.count() - 1, 1); - newstr.strList.append(str); - newstr.resultStr += str + "\n"; - str.clear(); - --i; + if (!str.isEmpty()) { + newstr.strList.append(str); + newstr.resultStr += str + "\n"; + str.clear(); + --i; + } else { + // 单字符宽度即超过label宽度,单独作为一行输出 + newstr.strList.append(strText.at(i)); + newstr.resultStr += strText.at(i) + QString("\n"); + str.clear(); + } } } newstr.strList.append(str); diff --git a/tests/document/ut_pdfmodel.cpp b/tests/document/ut_pdfmodel.cpp index 50d7670ae..74e399317 100644 --- a/tests/document/ut_pdfmodel.cpp +++ b/tests/document/ut_pdfmodel.cpp @@ -160,6 +160,29 @@ void allTextLooseRects_stub(void *, int &charCount, QStringList &texts, QVector< rects.append(QRectF(0, 20, 20, 10)); } +// BUG-383499 / CWE-125, CWE-129: charCount may be larger than the actual +// container sizes; words() must never index past texts/rects. +void allTextLooseRects_oversizedCharCount_stub(void *, int &charCount, QStringList &texts, QVector &rects) +{ + charCount = 10; // deliberately larger than the containers below + texts << "a" << "b" << "c"; + rects << QRectF(0, 0, 10, 10) << QRectF(12, 0, 10, 10) << QRectF(24, 0, 10, 10); +} + +void allTextLooseRects_rectsShorter_stub(void *, int &charCount, QStringList &texts, QVector &rects) +{ + charCount = 5; + texts << "a" << "b" << "c" << "d"; // texts is longer than rects + rects << QRectF(0, 0, 10, 10) << QRectF(12, 0, 10, 10); +} + +void allTextLooseRects_emptyContainers_stub(void *, int &charCount, QStringList &texts, QVector &rects) +{ + charCount = 5; // containers intentionally left empty + Q_UNUSED(texts); + Q_UNUSED(rects); +} + QVector search_stub(const QString &, bool, bool) { PageLine line; @@ -305,6 +328,49 @@ TEST_F(TestPDFPage, UT_PDFPage_words_001) EXPECT_TRUE(m_tester->words().size() == 3); } +// BUG-383499 / CWE-125, CWE-129: charCount larger than the containers must be +// clamped to the real container size, never indexing out of range. +TEST_F(TestPDFPage, UT_PDFPage_words_oversizedCharCount_002) +{ + Stub s; + s.set(static_cast &)>(ADDR(DPdfPage, allTextLooseRects)), allTextLooseRects_oversizedCharCount_stub); + m_tester->m_wordLoaded = false; + + const QList words = m_tester->words(); + + // Only the 3 real entries may be read (charCount lied with 10). + ASSERT_EQ(words.size(), 3); + EXPECT_EQ(words.at(0).text, QStringLiteral("a")); + EXPECT_EQ(words.at(1).text, QStringLiteral("b")); + EXPECT_EQ(words.at(2).text, QStringLiteral("c")); +} + +// BUG-383499: rects shorter than texts must also be bounded. +TEST_F(TestPDFPage, UT_PDFPage_words_rectsShorterThanTexts_003) +{ + Stub s; + s.set(static_cast &)>(ADDR(DPdfPage, allTextLooseRects)), allTextLooseRects_rectsShorter_stub); + m_tester->m_wordLoaded = false; + + const QList words = m_tester->words(); + + // Only the 2 rects have valid geometry; no OOB read of rects[2..3]. + ASSERT_EQ(words.size(), 2); + EXPECT_EQ(words.at(0).text, QStringLiteral("a")); + EXPECT_EQ(words.at(1).text, QStringLiteral("b")); +} + +// BUG-383499: non-positive containers with a positive charCount must not crash. +TEST_F(TestPDFPage, UT_PDFPage_words_emptyContainers_004) +{ + Stub s; + s.set(static_cast &)>(ADDR(DPdfPage, allTextLooseRects)), allTextLooseRects_emptyContainers_stub); + m_tester->m_wordLoaded = false; + + const QList words = m_tester->words(); + EXPECT_TRUE(words.isEmpty()); +} + TEST_F(TestPDFPage, UT_PDFPage_search_001) { typedef QVector (*searchPtr)(const QString &, bool, bool); diff --git a/tests/widgets/ut_securitydialog.cpp b/tests/widgets/ut_securitydialog.cpp index 4dc428eec..93789b95c 100644 --- a/tests/widgets/ut_securitydialog.cpp +++ b/tests/widgets/ut_securitydialog.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include class TestSecurityDialog : public ::testing::Test @@ -98,3 +99,27 @@ TEST(UT_autoCutText, longTextWraps) EXPECT_FALSE(result.resultStr.isEmpty()); EXPECT_GE(result.strList.size(), 1); } + +// BUG-383499 / CWE-835: when the label is narrower than a single glyph the +// wrapping loop used to decrement i, cancel the i++ increment and never exit. +TEST(UT_autoCutText, singleCharExceedsLabelWidthTerminates) +{ + DLabel label; + label.setFixedWidth(1); // narrower than any glyph + + const QString input = QStringLiteral("WideChar-测试-ABC"); + + QElapsedTimer timer; + timer.start(); + NewStr result = autoCutText(input, &label); + const qint64 elapsedMs = timer.elapsed(); + + // Must make progress and terminate instead of spinning forever. + EXPECT_LT(elapsedMs, 2000) << "autoCutText did not terminate (CWE-835 regression)"; + + // Every character must be preserved while wrapping. + QString joined = result.resultStr; + joined.remove(QLatin1Char('\n')); + EXPECT_EQ(joined, input); + EXPECT_FALSE(result.strList.isEmpty()); +}