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()); +}