--- kpdf/xpdf/xpdf/JPXStream.cc.orig 2005-12-04 13:01:07.000000000 +0100 +++ kpdf/xpdf/xpdf/JPXStream.cc 2005-12-04 13:15:44.000000000 +0100 @@ -666,7 +666,7 @@ int segType; GBool haveSIZ, haveCOD, haveQCD, haveSOT; Guint precinctSize, style; - Guint segLen, capabilities, comp, i, j, r; + Guint segLen, capabilities, nTiles, comp, i, j, r; //----- main header haveSIZ = haveCOD = haveQCD = haveSOT = gFalse; @@ -701,6 +701,13 @@ / img.xTileSize; img.nYTiles = (img.ySize - img.yTileOffset + img.yTileSize - 1) / img.yTileSize; + nTiles = img.nXTiles * img.nYTiles; + // check for overflow before allocating memory + if (nTiles == 0 || nTiles / img.nXTiles != img.nYTiles) { + error(getPos(), "Bad tile count in JPX SIZ marker segment"); + return gFalse; + } + img.tiles = (JPXTile *)gmalloc(nTiles * sizeof(JPXTile)); img.tiles = (JPXTile *)gmalloc(img.nXTiles * img.nYTiles * sizeof(JPXTile)); for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { --- kpdf/xpdf/xpdf/Stream.h.orig 2005-12-04 13:16:13.000000000 +0100 +++ kpdf/xpdf/xpdf/Stream.h 2005-12-04 13:21:52.000000000 +0100 @@ -233,6 +233,8 @@ ~StreamPredictor(); + GBool isOk() { return ok; } + int lookChar(); int getChar(); @@ -250,6 +252,7 @@ int rowBytes; // bytes per line Guchar *predLine; // line buffer int predIdx; // current index in predLine + GBool ok; }; //------------------------------------------------------------------------ --- kpdf/xpdf/xpdf/Stream.cc.orig 2005-12-04 13:23:51.000000000 +0100 +++ kpdf/xpdf/xpdf/Stream.cc 2005-12-04 13:36:34.000000000 +0100 @@ -408,18 +408,34 @@ StreamPredictor::StreamPredictor(Stream *strA, int predictorA, int widthA, int nCompsA, int nBitsA) { + int totalBits; + str = strA; predictor = predictorA; width = widthA; nComps = nCompsA; nBits = nBitsA; + predLine = NULL; + ok = gFalse; + nVals = width * nComps; + totalBits = nVals * nBits; + if (totalBits == 0 || + (totalBits / nBits) / nComps != width || + totalBits + 7 < 0) { + return; + } pixBytes = (nComps * nBits + 7) >> 3; - rowBytes = ((nVals * nBits + 7) >> 3) + pixBytes; + rowBytes = ((totalBits + 7) >> 3) + pixBytes; + if (rowBytes < 0) { + return; + } predLine = (Guchar *)gmalloc(rowBytes); memset(predLine, 0, rowBytes); predIdx = rowBytes; + + ok = gTrue; } StreamPredictor::~StreamPredictor() { @@ -1013,6 +1029,10 @@ FilterStream(strA) { if (predictor != 1) { pred = new StreamPredictor(this, predictor, columns, colors, bits); + if (!pred->isOk()) { + delete pred; + pred = NULL; + } } else { pred = NULL; } @@ -2899,6 +2919,14 @@ height = read16(); width = read16(); numComps = str->getChar(); + if (numComps <= 0 || numComps > 4) { + error(getPos(), "Bad number of components in DCT stream", prec); + return gFalse; + } + if (numComps <= 0 || numComps > 4) { + error(getPos(), "Bad number of components in DCT stream", prec); + return gFalse; + } if (prec != 8) { error(getPos(), "Bad DCT precision %d", prec); return gFalse; @@ -3258,6 +3286,10 @@ FilterStream(strA) { if (predictor != 1) { pred = new StreamPredictor(this, predictor, columns, colors, bits); + if (!pred->isOk()) { + delete pred; + pred = NULL; + } } else { pred = NULL; }