Skip to content

Commit 0005f44

Browse files
committed
#263: large allocation size following large columns value. limiting columns (and any integer) to max and mix pdf allowed value for integer to avoid
1 parent c22f9b0 commit 0005f44

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

PDFWriter/EncryptionHelper.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ EncryptionHelper::EncryptionHelper(void)
4848
mXcryptAuthentication = NULL;
4949
mXcryptStreams = NULL;
5050
mXcryptStrings = NULL;
51+
52+
mV = 0;
53+
mLength = 0;
54+
mRevision = 0;
55+
mP = 0;
56+
mEncryptMetaData = false;
5157
}
5258

5359
EncryptionHelper::~EncryptionHelper(void)

PDFWriter/IOBasicTypes.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ namespace IOBasicTypes
2828
typedef unsigned char Byte;
2929
typedef size_t LongBufferSizeType;
3030
typedef long long LongFilePositionType;
31-
}
31+
}
32+
33+

PDFWriter/InputPredictorTIFFSubStream.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ void InputPredictorTIFFSubStream::Assign(IByteReader* inSourceStream,
9898
mColumns = inColumns;
9999

100100
delete mRowBuffer;
101-
mRowBuffer = new Byte[(inColumns*inColors*inBitsPerComponent)/8];
101+
IOBasicTypes::LongBufferSizeType bufferSize = (inColumns*inColors*inBitsPerComponent)/8;
102+
mRowBuffer = new Byte[bufferSize];
102103

103104
mReadColorsCount = inColumns * inColors;
104105
mReadColors = new unsigned short[mReadColorsCount];

PDFWriter/PDFObjectParser.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
using namespace PDFHummus;
4747

48+
4849
PDFObjectParser::PDFObjectParser(void)
4950
{
5051
mParserExtender = NULL;
@@ -554,13 +555,27 @@ bool PDFObjectParser::IsNumber(const std::string& inToken)
554555

555556
typedef BoxingBaseWithRW<long long> LongLong;
556557

558+
// maximum allowed PDF int value = 2^31 − 1.
559+
#define MAX_PDF_INT 2147483647L
560+
// minimum allowed PDF int value = -2^31
561+
#define MIN_PDF_INT ((-MAX_PDF_INT)-1)
562+
557563
PDFObject* PDFObjectParser::ParseNumber(const std::string& inToken)
558564
{
559565
// once we know this is a number, then parsing is easy. just determine if it's a real or integer, so as to separate classes for better accuracy
560-
if(inToken.find(scDot) != inToken.npos)
566+
if(inToken.find(scDot) != inToken.npos) {
561567
return new PDFReal(Double(inToken));
562-
else
563-
return new PDFInteger(LongLong(inToken));
568+
} else {
569+
long long integerValue = LongLong(inToken);
570+
571+
// validate int value according to PDF limits. ignore if outside of range
572+
if((integerValue > MAX_PDF_INT) || (integerValue < MIN_PDF_INT)) {
573+
TRACE_LOG3("PDFObjectParser::ParseNumber, parsed integer %lld is outside of the allowed range for PDF integers - %ld to %ld", integerValue, MIN_PDF_INT, MAX_PDF_INT);
574+
return NULL;
575+
}
576+
577+
return new PDFInteger(integerValue);
578+
}
564579
}
565580

566581
static const std::string scLeftSquare = "[";

PDFWriter/PDFParser.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,20 @@ EStatusCodeAndIByteReader PDFParser::WrapWithPredictorStream(IByteReader* inputS
20062006
(IOBasicTypes::LongBufferSizeType)bitsPerComponent->GetValue() :
20072007
8;
20082008

2009+
// validate bits per component
2010+
if(
2011+
bitsPerComponentValue != 1 &&
2012+
bitsPerComponentValue != 2 &&
2013+
bitsPerComponentValue != 4 &&
2014+
bitsPerComponentValue != 8 &&
2015+
bitsPerComponentValue != 16
2016+
) {
2017+
TRACE_LOG1("PDFParser::WrapWithPredictorStream, invalid BitsPerComponent value: %ld. allowed values: 1,2,4,8,16", bitsPerComponentValue);
2018+
status = PDFHummus::eFailure;
2019+
break;
2020+
}
2021+
2022+
20092023
switch(predictor->GetValue())
20102024
{
20112025
case 2:
Binary file not shown.

0 commit comments

Comments
 (0)