Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions reader/document/PDFModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ QList<Word> 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;
}
Expand All @@ -197,7 +197,7 @@ QList<Word> PDFPage::words()
QList<Word> 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;
Expand Down
17 changes: 12 additions & 5 deletions reader/widgets/SecurityDialog.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
Expand Down
66 changes: 66 additions & 0 deletions tests/document/ut_pdfmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QRectF> &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<QRectF> &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<QRectF> &rects)
{
charCount = 5; // containers intentionally left empty
Q_UNUSED(texts);
Q_UNUSED(rects);
}

QVector<PageSection> search_stub(const QString &, bool, bool)
{
PageLine line;
Expand Down Expand Up @@ -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<void(DPdfPage::*)(int &, QStringList &, QVector<QRectF> &)>(ADDR(DPdfPage, allTextLooseRects)), allTextLooseRects_oversizedCharCount_stub);
m_tester->m_wordLoaded = false;

const QList<Word> 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<void(DPdfPage::*)(int &, QStringList &, QVector<QRectF> &)>(ADDR(DPdfPage, allTextLooseRects)), allTextLooseRects_rectsShorter_stub);
m_tester->m_wordLoaded = false;

const QList<Word> 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<void(DPdfPage::*)(int &, QStringList &, QVector<QRectF> &)>(ADDR(DPdfPage, allTextLooseRects)), allTextLooseRects_emptyContainers_stub);
m_tester->m_wordLoaded = false;

const QList<Word> words = m_tester->words();
EXPECT_TRUE(words.isEmpty());
}

TEST_F(TestPDFPage, UT_PDFPage_search_001)
{
typedef QVector<PageSection> (*searchPtr)(const QString &, bool, bool);
Expand Down
25 changes: 25 additions & 0 deletions tests/widgets/ut_securitydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "stub.h"

#include <gtest/gtest.h>
#include <QTest>

Check warning on line 11 in tests/widgets/ut_securitydialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QSignalSpy>

Check warning on line 12 in tests/widgets/ut_securitydialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QSignalSpy> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEvent>

Check warning on line 13 in tests/widgets/ut_securitydialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QElapsedTimer>

Check warning on line 14 in tests/widgets/ut_securitydialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QElapsedTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLabel>

Check warning on line 15 in tests/widgets/ut_securitydialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLabel> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class TestSecurityDialog : public ::testing::Test
{
Expand Down Expand Up @@ -98,3 +99,27 @@
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());
}
Loading