Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Add a unittest for RGBE overflow fix

Dmitry Kazakov377889448bd9
diff --git a/plugins/impex/rgbe/RGBEImportUtils.cpp b/plugins/impex/rgbe/RGBEImportUtils.cpp
index 6730f88326..b7515da8e5 100644
--- a/plugins/impex/rgbe/RGBEImportUtils.cpp
+++ b/plugins/impex/rgbe/RGBEImportUtils.cpp
@@ -31,12 +31,14 @@ bool ReadOldLine(quint8 *image, int width, QDataStream &s)
s >> image[0];
s >> image[1];
s >> image[2];
- s >> image[3];
+ // check if we can read the whole pixel block correctly
if (s.atEnd()) {
return false;
}
+ s >> image[3];
+
if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) {
const int length = image[3] << rshift;
if (length > width) {
@@ -101,12 +103,12 @@ bool LoadHDR(QDataStream &s, const int width, const int height, KisSequentialIte
continue;
}
- s >> val;
-
if (s.atEnd()) {
- return true;
+ return false;
}
+ s >> val;
+
if (val != 2) {
s.device()->ungetChar(val);
if (!ReadOldLine(image, width, s)) {
@@ -118,12 +120,14 @@ bool LoadHDR(QDataStream &s, const int width, const int height, KisSequentialIte
s >> image[1];
s >> image[2];
- s >> image[3];
+ // check if we can read the whole pixel block correctly
if (s.atEnd()) {
- return true;
+ return false;
}
+ s >> image[3];
+
if ((image[1] != 2) || (image[2] & 128)) {
image[0] = 2;
if (!ReadOldLine(image + 4, width - 1, s)) {
diff --git a/plugins/impex/rgbe/tests/CMakeLists.txt b/plugins/impex/rgbe/tests/CMakeLists.txt
index 029f816264..2ed61de123 100644
--- a/plugins/impex/rgbe/tests/CMakeLists.txt
+++ b/plugins/impex/rgbe/tests/CMakeLists.txt
@@ -1,5 +1,5 @@
kis_add_test(
kis_rgbe_test.cpp
TEST_NAME kis_rgbe_test
- LINK_LIBRARIES kritametadata kritaui kritatestsdk
+ LINK_LIBRARIES kritametadata kritaui kritatestsdk kritargbeimportutils
)
diff --git a/plugins/impex/rgbe/tests/kis_rgbe_test.cpp b/plugins/impex/rgbe/tests/kis_rgbe_test.cpp
index 1f9891d419..d1fe81dff3 100644
--- a/plugins/impex/rgbe/tests/kis_rgbe_test.cpp
+++ b/plugins/impex/rgbe/tests/kis_rgbe_test.cpp
@@ -10,6 +10,10 @@
#include <QString>
+#include <QBuffer>
+#include <QDataStream>
+#include <RGBEImportUtils.h>
+
#include <filestest.h>
#include <kis_meta_data_backend_registry.h>
#include <testui.h>
@@ -75,6 +79,225 @@ void KisRGBETest::testSaveRgbaColorSpace()
testSaveColorSpace(RGBAColorModelID.id(), Float32BitsColorDepthID.id(), profile);
}
+void KisRGBETest::testRLEOverflow_data()
+{
+ QTest::addColumn<QByteArray>("srcData");
+ QTest::addColumn<bool>("expectedSuccess");
+
+ struct DataGenerator {
+ QByteArray component0;
+
+ QByteArray generate() const {
+ QByteArray srcData;
+
+ // Construct HDR RLE data that attempts buffer overflow
+ // Format: [2] [2] [width_msb] [width_lsb] [RLE_component_0] [RLE_component_1] [RLE_component_2]
+ // [RLE_component_3]
+
+ srcData.append(static_cast<char>(2)); // Header marker for new scanline format
+ srcData.append(static_cast<char>(2)); // Confirms new format
+ srcData.append(static_cast<char>(0x00)); // Width MSB (16 = 0x0010)
+ srcData.append(static_cast<char>(0x10)); // Width LSB
+
+ srcData.append(component0);
+
+ // Components 1, 2, 3: Add valid RLE data to reach end of stream
+ for (int i = 1; i < 4; i++) {
+ srcData.append(static_cast<char>(16)); // Read 16 bytes in non-run mode
+ for (int j = 0; j < 16; ++j) {
+ srcData.append(static_cast<char>(0x80 + i)); // Dummy data byte
+ }
+ }
+
+ return srcData;
+ }
+ };
+
+ struct OldDataGenerator {
+ QByteArray component0;
+
+ QByteArray generate() const {
+ QByteArray srcData;
+
+ // Construct HDR RLE data that attempts buffer overflow
+ // Format: [2] [2] [width_msb] [width_lsb] [RLE_component_0] [RLE_component_1] [RLE_component_2]
+ // [RLE_component_3]
+
+ srcData.append(static_cast<char>(2)); // Header marker for new scanline format
+ srcData.append(static_cast<char>(2)); // Confirms new format
+ srcData.append(static_cast<char>(0x00)); // Width MSB (16 = 0x0010)
+ srcData.append(static_cast<char>(0x10)); // Width LSB
+
+ srcData.append(component0);
+
+ // Components 1, 2, 3: Add valid RLE data to reach end of stream
+ for (int i = 1; i < 4; i++) {
+ srcData.append(static_cast<char>(16)); // Read 16 bytes in non-run mode
+ for (int j = 0; j < 16; ++j) {
+ srcData.append(static_cast<char>(0x80 + i)); // Dummy data byte
+ }
+ }
+
+ return srcData;
+ }
+ };
+
+
+ {
+ DataGenerator g;
+
+ // Component 0: RLE run that attempts overflow
+ g.component0.append(static_cast<char>(72 | 128)); // RLE run marker is 128, repeat 72 times
+ g.component0.append(static_cast<char>(0x80)); // Value to repeat
+
+ QTest::addRow("rle-deep-past-the-end") << g.generate() << false;
+ }
+
+ {
+ DataGenerator g;
+
+ // Component 0: RLE run that attempts overflow
+ g.component0.append(static_cast<char>(17 | 128)); // RLE run marker is 128, repeat 17 times
+ g.component0.append(static_cast<char>(0x80)); // Value to repeat
+
+ QTest::addRow("rle-1byte-past-the-end") << g.generate() << false;
+ }
+
+ {
+ DataGenerator g;
+
+ // Component 0: RLE run that attempts overflow
+ g.component0.append(static_cast<char>(1)); // Directly copy one byte
+ g.component0.append(static_cast<char>(0x70));
+
+ g.component0.append(static_cast<char>(16 | 128)); // RLE run marker is 128, repeat 16 times
+ g.component0.append(static_cast<char>(0x80)); // Value to repeat
+
+ QTest::addRow("rle-1byte-past-the-end-with-offset") << g.generate() << false;
+ }
+
+ {
+ DataGenerator g;
+
+ // Component 0: RLE run that expands to exactly 16 pixels
+ g.component0.append(static_cast<char>(16 | 128)); // RLE run marker is 128, repeat 16 times
+ g.component0.append(static_cast<char>(0x80)); // Value to repeat
+
+ QTest::addRow("rle-correct") << g.generate() << true;
+ }
+
+ {
+ DataGenerator g;
+
+ // Component 0: RLE run that expands to exactly 16 pixels
+ g.component0.append(static_cast<char>(1)); // Directly copy one byte
+ g.component0.append(static_cast<char>(0x70));
+
+ g.component0.append(static_cast<char>(15 | 128)); // RLE run marker is 128, repeat 15 times
+ g.component0.append(static_cast<char>(0x80)); // Value to repeat
+
+ QTest::addRow("rle-correct-with-offset") << g.generate() << true;
+ }
+
+ {
+ DataGenerator g;
+
+ // Component 0: non-run mode that attempts overflow
+ g.component0.append(static_cast<char>(17)); // Directly copy 17 bytes
+ for (int j = 0; j < 17; j++) {
+ g.component0.append(static_cast<char>(0x70));
+ }
+
+ QTest::addRow("non-run-1byte-past-the-end") << g.generate() << false;
+ }
+
+ {
+ DataGenerator g;
+
+ // Component 0: non-run mode that expands to exactly 16 pixels
+ g.component0.append(static_cast<char>(16)); // Directly copy 16 bytes
+ for (int j = 0; j < 16; j++) {
+ g.component0.append(static_cast<char>(0x70));
+ }
+
+ QTest::addRow("non-run-correct") << g.generate() << true;
+ }
+
+ {
+ QByteArray srcData;
+ // Each record is one HDR pixel in RGBe form: [R][G][B][E],
+ // pixel [1][1][1][count] is a special marker to repeat the
+ // previous pixel `count` times
+
+ for (int j = 0; j < 16; j++) {
+ srcData.append(static_cast<char>(0x80));
+ srcData.append(static_cast<char>(0x81));
+ srcData.append(static_cast<char>(0x82));
+ srcData.append(static_cast<char>(0x83));
+ }
+
+ QTest::addRow("old-format-uncompressed") << srcData << true;
+ }
+
+ {
+ QByteArray srcData;
+ // Each record is one HDR pixel in RGBe form: [R][G][B][E],
+ // pixel [1][1][1][count] is a special marker to repeat the
+ // previous pixel `count` times
+
+ srcData.append(static_cast<char>(0x80));
+ srcData.append(static_cast<char>(0x81));
+ srcData.append(static_cast<char>(0x82));
+ srcData.append(static_cast<char>(0x83));
+ srcData.append(static_cast<char>(1));
+ srcData.append(static_cast<char>(1));
+ srcData.append(static_cast<char>(1));
+ srcData.append(static_cast<char>(15));
+
+ QTest::addRow("old-format-expand-exact") << srcData << true;
+ }
+
+ {
+ QByteArray srcData;
+ // Each record is one HDR pixel in RGBe form: [R][G][B][E],
+ // pixel [1][1][1][count] is a special marker to repeat the
+ // previous pixel `count` times
+
+ srcData.append(static_cast<char>(0x80));
+ srcData.append(static_cast<char>(0x81));
+ srcData.append(static_cast<char>(0x82));
+ srcData.append(static_cast<char>(0x83));
+ srcData.append(static_cast<char>(1));
+ srcData.append(static_cast<char>(1));
+ srcData.append(static_cast<char>(1));
+ srcData.append(static_cast<char>(16));
+
+ QTest::addRow("old-format-1px-past-the-end") << srcData << false;
+ }
+
+}
+
+void KisRGBETest::testRLEOverflow()
+{
+ //QByteArray dstData;
+ QFETCH(QByteArray, srcData);
+ QFETCH(bool, expectedSuccess);
+
+ const int width = 16;
+
+ const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();
+ KisPaintDeviceSP device = new KisPaintDevice(cs);
+ KisSequentialIterator it(device, {0, 0, width, 1});
+
+ QBuffer buffer(&srcData);
+ buffer.open(QIODevice::ReadOnly);
+
+ QDataStream stream(&buffer);
+ bool result = RGBEIMPORT::LoadHDR(stream, width, 1, it);
+
+ QCOMPARE(result, expectedSuccess);
+}
+
#ifndef _WIN32
void KisRGBETest::testImportFromWriteonly()
{
diff --git a/plugins/impex/rgbe/tests/kis_rgbe_test.h b/plugins/impex/rgbe/tests/kis_rgbe_test.h
index 2ae8c5e900..39e5a203b5 100644
--- a/plugins/impex/rgbe/tests/kis_rgbe_test.h
+++ b/plugins/impex/rgbe/tests/kis_rgbe_test.h
@@ -17,6 +17,8 @@ private Q_SLOTS:
void testFiles();
void testHDR();
void testSaveRgbaColorSpace();
+ void testRLEOverflow_data();
+ void testRLEOverflow();
void testImportIncorrectFormat();
#ifndef Q_OS_WIN