Browse Source

TgaImporter: check if the file is long enough.

Prevents undefined behavior when opening e.g. empty files.
pull/34/head
Vladimír Vondruš 14 years ago
parent
commit
1f4e6022ac
  1. 12
      src/Plugins/TgaImporter/Test/TgaImporterTest.cpp
  2. 1
      src/Plugins/TgaImporter/Test/TgaImporterTest.h
  3. 9
      src/Plugins/TgaImporter/TgaImporter.cpp

12
src/Plugins/TgaImporter/Test/TgaImporterTest.cpp

@ -36,6 +36,18 @@ void TgaImporterTest::openInexistent() {
QCOMPARE(debug.str().c_str(), "TgaImporter: cannot open file inexistent.file\n"); QCOMPARE(debug.str().c_str(), "TgaImporter: cannot open file inexistent.file\n");
} }
void TgaImporterTest::openShort() {
const char data[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
istringstream in(string(data, sizeof(data)));
ostringstream debug;
Error::setOutput(&debug);
TgaImporter importer;
QVERIFY(!importer.open(in));
QCOMPARE(debug.str().c_str(), "TgaImporter: the file is too short: 17 bytes\n");
}
void TgaImporterTest::paletted() { void TgaImporterTest::paletted() {
const char data[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; const char data[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
istringstream in(string(data, sizeof(data))); istringstream in(string(data, sizeof(data)));

1
src/Plugins/TgaImporter/Test/TgaImporterTest.h

@ -24,6 +24,7 @@ class TgaImporterTest: public QObject {
private slots: private slots:
void openInexistent(); void openInexistent();
void openShort();
void paletted(); void paletted();
void nonRgb(); void nonRgb();
void bits16(); void bits16();

9
src/Plugins/TgaImporter/TgaImporter.cpp

@ -47,6 +47,15 @@ bool TgaImporter::open(std::istream& in) {
return false; return false;
} }
/* Check if the file is long enough */
in.seekg(0, istream::end);
streampos filesize = in.tellg();
in.seekg(0, istream::beg);
if(filesize < streampos(sizeof(Header))) {
Error() << "TgaImporter: the file is too short:" << filesize << "bytes";
return false;
}
Header header; Header header;
in.read(reinterpret_cast<char*>(&header), sizeof(Header)); in.read(reinterpret_cast<char*>(&header), sizeof(Header));

Loading…
Cancel
Save