1 diff -u -p -Nr --exclude CVS xpdf/JBIG2Stream.cc xpdf/JBIG2Stream.cc
2 --- xpdf/JBIG2Stream.cc 2004-05-17 20:11:43.000000000 +0200
3 +++ xpdf/JBIG2Stream.cc 2005-12-15 13:38:04.000000000 +0100
5 //========================================================================
10 #ifdef USE_GCC_PRAGMAS
11 #pragma implementation
12 @@ -681,7 +682,14 @@ JBIG2Bitmap::JBIG2Bitmap(Guint segNumA,
16 - data = (Guchar *)gmalloc(h * line);
18 + if (h < 0 || line <= 0 || h >= (INT_MAX-1) / line)
21 + // need to allocate one extra guard byte for use in combine()
22 + data = (Guchar *)gmalloc(h * line + 1);
27 JBIG2Bitmap::JBIG2Bitmap(Guint segNumA, JBIG2Bitmap *bitmap):
28 @@ -690,8 +698,16 @@ JBIG2Bitmap::JBIG2Bitmap(Guint segNumA,
32 - data = (Guchar *)gmalloc(h * line);
34 + if (h < 0 || line <= 0 || h >= (INT_MAX-1) / line) {
39 + data = (Guchar *)gmalloc(h * line + 1);
41 memcpy(data, bitmap->data, h * line);
45 JBIG2Bitmap::~JBIG2Bitmap() {
46 @@ -716,10 +732,10 @@ JBIG2Bitmap *JBIG2Bitmap::getSlice(Guint
49 void JBIG2Bitmap::expand(int newH, Guint pixel) {
51 + if (newH <= h || line <= 0 || newH >= (INT_MAX-1) / line) {
54 - data = (Guchar *)grealloc(data, newH * line);
55 + data = (Guchar *)grealloc(data, newH * line + 1);
57 memset(data + h * line, 0xff, (newH - h) * line);
59 @@ -2256,6 +2272,16 @@ void JBIG2Stream::readHalftoneRegionSeg(
60 error(getPos(), "Bad symbol dictionary reference in JBIG2 halftone segment");
64 + if (gridH == 0 || gridW >= INT_MAX / gridH) {
65 + error(getPos(), "Bad size in JBIG2 halftone segment");
68 + if (w == 0 || h >= INT_MAX / w) {
69 + error(getPos(), "Bad size in JBIG2 bitmap segment");
73 patternDict = (JBIG2PatternDict *)seg;
76 @@ -2887,6 +2913,9 @@ JBIG2Bitmap *JBIG2Stream::readGenericRef
77 JBIG2BitmapPtr tpgrCXPtr0, tpgrCXPtr1, tpgrCXPtr2;
80 + if (w < 0 || h <= 0 || w >= INT_MAX / h)
83 bitmap = new JBIG2Bitmap(0, w, h);
84 bitmap->clearToZero();
86 diff -u -p -Nr --exclude CVS xpdf/JPXStream.cc xpdf/JPXStream.cc
87 --- xpdf/JPXStream.cc 2004-05-17 20:11:49.000000000 +0200
88 +++ xpdf/JPXStream.cc 2005-12-15 13:23:59.000000000 +0100
90 //========================================================================
95 #ifdef USE_GCC_PRAGMAS
96 #pragma implementation
97 @@ -666,7 +667,7 @@ GBool JPXStream::readCodestream(Guint le
99 GBool haveSIZ, haveCOD, haveQCD, haveSOT;
100 Guint precinctSize, style;
101 - Guint segLen, capabilities, comp, i, j, r;
102 + Guint segLen, capabilities, nTiles, comp, i, j, r;
105 haveSIZ = haveCOD = haveQCD = haveSOT = gFalse;
106 @@ -701,7 +702,19 @@ GBool JPXStream::readCodestream(Guint le
108 img.nYTiles = (img.ySize - img.yTileOffset + img.yTileSize - 1)
110 - img.tiles = (JPXTile *)gmalloc(img.nXTiles * img.nYTiles *
111 + // check for overflow before allocating memory
112 + if (img.nXTiles <= 0 || img.nYTiles <= 0 ||
113 + img.nXTiles >= INT_MAX/img.nYTiles) {
114 + error(getPos(), "Bad tile count in JPX SIZ marker segment");
117 + nTiles = img.nXTiles * img.nYTiles;
118 + // check for overflow before allocating memory
119 + if (nTiles == 0 || nTiles >= INT_MAX/sizeof(JPXTile)) {
120 + error(getPos(), "Bad tile count in JPX SIZ marker segment");
123 + img.tiles = (JPXTile *)gmalloc(nTiles *
125 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
126 img.tiles[i].tileComps = (JPXTileComp *)gmalloc(img.nComps *
127 diff -u -p -Nr --exclude CVS xpdf/Stream.cc xpdf/Stream.cc
128 --- xpdf/Stream.cc 2004-05-17 21:37:57.000000000 +0200
129 +++ xpdf/Stream.cc 2005-12-15 13:40:45.000000000 +0100
138 @@ -407,18 +408,41 @@ void ImageStream::skipLine() {
140 StreamPredictor::StreamPredictor(Stream *strA, int predictorA,
141 int widthA, int nCompsA, int nBitsA) {
145 predictor = predictorA;
152 + if (width <= 0 || nComps <= 0 || nBits <= 0 ||
153 + nComps >= INT_MAX/nBits ||
154 + width >= INT_MAX/nComps/nBits) {
157 nVals = width * nComps;
158 + if (nVals + 7 <= 0) {
161 + totalBits = nVals * nBits;
162 + if (totalBits == 0 ||
163 + (totalBits / nBits) / nComps != width ||
164 + totalBits + 7 < 0) {
167 pixBytes = (nComps * nBits + 7) >> 3;
168 - rowBytes = ((nVals * nBits + 7) >> 3) + pixBytes;
169 + rowBytes = ((totalBits + 7) >> 3) + pixBytes;
170 + if (rowBytes < 0) {
173 predLine = (Guchar *)gmalloc(rowBytes);
174 memset(predLine, 0, rowBytes);
180 StreamPredictor::~StreamPredictor() {
181 @@ -1012,6 +1036,10 @@ LZWStream::LZWStream(Stream *strA, int p
183 if (predictor != 1) {
184 pred = new StreamPredictor(this, predictor, columns, colors, bits);
185 + if (!pred->isOk()) {
192 @@ -1260,6 +1288,10 @@ CCITTFaxStream::CCITTFaxStream(Stream *s
193 endOfLine = endOfLineA;
194 byteAlign = byteAlignA;
196 + if (columns + 4 < 1 || (columns + 4) >= INT_MAX / sizeof(short)) {
197 + error(getPos(), "Bad number of columns in CCITTFaxStream");
201 endOfBlock = endOfBlockA;
203 @@ -2897,6 +2929,11 @@ GBool DCTStream::readBaselineSOF() {
206 numComps = str->getChar();
207 + if (numComps <= 0 || numComps > 4) {
209 + error(getPos(), "Bad number of components in DCT stream", prec);
213 error(getPos(), "Bad DCT precision %d", prec);
215 @@ -2923,6 +2960,11 @@ GBool DCTStream::readProgressiveSOF() {
218 numComps = str->getChar();
219 + if (numComps <= 0 || numComps > 4) {
221 + error(getPos(), "Bad number of components in DCT stream");
225 error(getPos(), "Bad DCT precision %d", prec);
227 @@ -2945,6 +2987,11 @@ GBool DCTStream::readScanInfo() {
229 length = read16() - 2;
230 scanInfo.numComps = str->getChar();
231 + if (scanInfo.numComps <= 0 || scanInfo.numComps > 4) {
232 + scanInfo.numComps = 0;
233 + error(getPos(), "Bad number of components in DCT stream");
237 if (length != 2 * scanInfo.numComps + 3) {
238 error(getPos(), "Bad DCT scan info block");
239 @@ -3019,12 +3066,12 @@ GBool DCTStream::readHuffmanTables() {
241 index = str->getChar();
243 - if ((index & 0x0f) >= 4) {
244 + if ((index & ~0x10) >= 4 || (index & ~0x10) < 0) {
245 error(getPos(), "Bad DCT Huffman table");
251 if (index >= numACHuffTables)
252 numACHuffTables = index+1;
253 tbl = &acHuffTables[index];
254 @@ -3255,6 +3302,10 @@ FlateStream::FlateStream(Stream *strA, i
256 if (predictor != 1) {
257 pred = new StreamPredictor(this, predictor, columns, colors, bits);
258 + if (!pred->isOk()) {
265 diff -u -p -Nr --exclude CVS xpdf/Stream.h xpdf/Stream.h
266 --- xpdf/Stream.h 2004-05-17 21:37:57.000000000 +0200
267 +++ xpdf/Stream.h 2005-12-15 13:23:59.000000000 +0100
268 @@ -233,6 +233,8 @@ public:
272 + GBool isOk() { return ok; }
277 @@ -250,6 +252,7 @@ private:
278 int rowBytes; // bytes per line
279 Guchar *predLine; // line buffer
280 int predIdx; // current index in predLine
284 //------------------------------------------------------------------------