From 6611e91b52506820b9ded09f0c45810ab7c4ad98 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Mon, 17 Aug 2015 16:58:21 +0200 Subject: afl: improve sanity check Change-Id: I58d8100c4e6e5d15464df6868625fffe8b51d947 --- src/lib/CDRParser.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/lib/CDRParser.cpp') diff --git a/src/lib/CDRParser.cpp b/src/lib/CDRParser.cpp index c6eabc9..101d9bf 100644 --- a/src/lib/CDRParser.cpp +++ b/src/lib/CDRParser.cpp @@ -1636,11 +1636,18 @@ void libcdr::CDRParser::readTrfd(librevenge::RVNGInputStream *input, unsigned le if (!_redirectX6Chunk(&input, length)) throw GenericException(); long startPosition = input->tell(); + const unsigned long maxLength = getLength(input); + if (startPosition >= long(maxLength)) + return; + if ((length > maxLength) || (long(maxLength - length) < startPosition)) + length = unsigned(maxLength - static_cast(startPosition)); // sanitize length unsigned chunkLength = readUnsigned(input); unsigned numOfArgs = readUnsigned(input); - if (numOfArgs > length / 4) // avoid extra big allocation in case of a broken file - numOfArgs = length / 4; unsigned startOfArgs = readUnsigned(input); + if (startOfArgs >= length) + return; + if (numOfArgs > (length - startOfArgs) / 4) // avoid extra big allocation in case of a broken file + numOfArgs = (length - startOfArgs) / 4; std::vector argOffsets(numOfArgs, 0); unsigned i = 0; input->seek(startPosition+startOfArgs, librevenge::RVNG_SEEK_SET); -- cgit v1.2.3