Skip to content
Permalink
Browse files Browse the repository at this point in the history
PdfXRefStreamParserObject: Fixed handling of invalid XRef stream entries
Should fix #69
  • Loading branch information
ceztko committed Apr 21, 2023
1 parent c52bbff commit 535a786
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion playground/CMakeLists.txt
Expand Up @@ -8,7 +8,7 @@ endif()
unset(CMAKE_CONFIGURATION_TYPES CACHE) # It will also remove it from GUI
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}")

project(PoDoFo)
project(podofo)
enable_testing() # Needed to enable "make test" from binary dir

# An useful variable to check if we are running from playground
Expand Down
24 changes: 12 additions & 12 deletions src/podofo/main/PdfXRefStreamParserObject.cpp
Expand Up @@ -67,15 +67,15 @@ void PdfXRefStreamParserObject::ReadXRefTable()
// all of them have to be integers
const PdfArray* arr;
if (!arrObj.TryGetArray(arr) || arr->size() != 3)
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef, "Invalid XRef stream /W array");
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream, "Invalid XRef stream /W array");

int64_t wArray[W_ARRAY_SIZE] = { 0, 0, 0 };
int64_t num;
for (unsigned i = 0; i < W_ARRAY_SIZE; i++)
{

if (!(*arr)[i].TryGetNumber(num))
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef, "Invalid XRef stream /W array");
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream, "Invalid XRef stream /W array");

wArray[i] = num;
}
Expand All @@ -92,12 +92,12 @@ void PdfXRefStreamParserObject::parseStream(const int64_t wArray[W_ARRAY_SIZE],
{
if (wArray[i] < 0)
{
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef,
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream,
"Negative field length in XRef stream");
}
if (numeric_limits<int64_t>::max() - lengthSum < wArray[i])
{
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef,
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream,
"Invalid entry length in XRef stream");
}
else
Expand All @@ -112,24 +112,24 @@ void PdfXRefStreamParserObject::parseStream(const int64_t wArray[W_ARRAY_SIZE],
this->GetOrCreateStream().CopyTo(buffer);

vector<int64_t>::const_iterator it = indices.begin();
char* cursor = buffer.data();
size_t offset = 0;
while (it != indices.end())
{
int64_t firstObj = *it++;
int64_t count = *it++;

if ((offset + count * entryLen) > buffer.size())
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream, "Invalid count in XRef stream");

m_entries->Enlarge(firstObj + count);
for (unsigned index = 0; index < (unsigned)count; index++)
{
if ((size_t)(cursor - buffer.data()) >= buffer.size())
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef, "Invalid count in XRef stream");

unsigned objIndex = (unsigned)firstObj + index;
auto& entry = (*m_entries)[objIndex];
if (objIndex < m_entries->GetSize() && !entry.Parsed)
readXRefStreamEntry(entry, cursor, wArray);
readXRefStreamEntry(entry, buffer.data() + offset, wArray);

cursor += entryLen;
offset += entryLen;
}
}
}
Expand All @@ -149,15 +149,15 @@ void PdfXRefStreamParserObject::getIndices(vector<int64_t>& indices, int64_t siz
{
const PdfArray* arr;
if (!indexObj->TryGetArray(arr))
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef, "Invalid XRef Stream /Index");
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream, "Invalid XRef Stream /Index");

for (auto index : *arr)
indices.push_back(index.GetNumber());
}

// indices must be a multiple of 2
if (indices.size() % 2 != 0)
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::NoXRef, "Invalid XRef Stream /Index");
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidXRefStream, "Invalid XRef Stream /Index");
}

void PdfXRefStreamParserObject::readXRefStreamEntry(PdfXRefEntry& entry, char* buffer, const int64_t wArray[W_ARRAY_SIZE])
Expand Down
14 changes: 7 additions & 7 deletions test/unit/ParserTest.cpp
Expand Up @@ -1060,7 +1060,7 @@ void testReadXRefSubsection()
}
}

TEST_CASE("testReadXRefStreamContents")
TEST_CASE("TestReadXRefStreamContents")
{
// test valid stream
try
Expand Down Expand Up @@ -1157,7 +1157,7 @@ TEST_CASE("testReadXRefStreamContents")
}
catch (PdfError& error)
{
REQUIRE(error.GetCode() == PdfErrorCode::NoXRef);
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream);
}
catch (exception&)
{
Expand Down Expand Up @@ -1210,7 +1210,7 @@ TEST_CASE("testReadXRefStreamContents")
}
catch (PdfError& error)
{
REQUIRE(error.GetCode() == PdfErrorCode::NoXRef);
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream);
}
catch (exception&)
{
Expand Down Expand Up @@ -1314,7 +1314,7 @@ TEST_CASE("testReadXRefStreamContents")
}
catch (PdfError& error)
{
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefType);
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream);
}
catch (exception&)
{
Expand Down Expand Up @@ -1363,7 +1363,7 @@ TEST_CASE("testReadXRefStreamContents")
}
catch (PdfError& error)
{
REQUIRE(error.GetCode() == PdfErrorCode::NoXRef);
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream);
}
catch (exception&)
{
Expand Down Expand Up @@ -1722,7 +1722,7 @@ TEST_CASE("testReadXRefStreamContents")
}
catch (PdfError& error)
{
REQUIRE(error.GetCode() == PdfErrorCode::NoXRef);
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream);
}
catch (exception&)
{
Expand Down Expand Up @@ -1772,7 +1772,7 @@ TEST_CASE("testReadXRefStreamContents")
}
catch (PdfError& error)
{
REQUIRE(error.GetCode() == PdfErrorCode::NoXRef);
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream);
}
catch (exception&)
{
Expand Down

0 comments on commit 535a786

Please sign in to comment.