From 66326f06186f695c9999c1eb1ed43378dd7f63d8 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Fri, 21 Sep 2012 01:36:26 +0000 Subject: Fix bug from a few minutes ago git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1737 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- converter/ppm/xpmtoppm.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'converter/ppm/xpmtoppm.c') diff --git a/converter/ppm/xpmtoppm.c b/converter/ppm/xpmtoppm.c index 0f51e9fd..a423de50 100644 --- a/converter/ppm/xpmtoppm.c +++ b/converter/ppm/xpmtoppm.c @@ -177,6 +177,7 @@ getLine(char * const line, static void getColorNumber(const char * const pArg, unsigned int const bytesPerPixel, + unsigned int const nColors, unsigned int * const colorNumberP, unsigned int * const bytesReadP) { /*---------------------------------------------------------------------------- @@ -206,6 +207,11 @@ getColorNumber(const char * const pArg, accum += *q; } + if (bytesPerPixel <= 2 && accum >= nColors) + pm_error("Color number %u in color map is too large, as the " + "header says there are only %u colors in the image", + accum, nColors); + *colorNumberP = accum; *bytesReadP = q - p; } @@ -326,7 +332,7 @@ interpretXpm3ColorTableLine(char const line[], else ++t1; /* Points now to first color number character */ - getColorNumber(t1, charsPerPixel, &colorNumber, &bytesRead); + getColorNumber(t1, charsPerPixel, nColors, &colorNumber, &bytesRead); if (bytesRead < charsPerPixel) pm_error("A color map entry ends in the middle of the colormap index"); @@ -613,7 +619,8 @@ readV1ColorTable(FILE * const ifP, unsigned int colorNumber; unsigned int bytesRead; - getColorNumber(str1, charsPerPixel, &colorNumber, &bytesRead); + getColorNumber(str1, charsPerPixel, nColors, + &colorNumber, &bytesRead); if (bytesRead < charsPerPixel) pm_error("A color map entry ends in the middle " @@ -741,7 +748,7 @@ static void getColormapIndex(const char ** const lineCursorP, unsigned int const charsPerPixel, unsigned int * const ptab, - unsigned int const ptabSize, + unsigned int const nColors, unsigned int * const colormapIndexP) { /*---------------------------------------------------------------------------- Read from the line (advancing cursor *lineCursorP) the next @@ -757,7 +764,8 @@ getColormapIndex(const char ** const lineCursorP, unsigned int colorNumber; unsigned int bytesRead; - getColorNumber(pixelBytes, charsPerPixel, &colorNumber, &bytesRead); + getColorNumber(pixelBytes, charsPerPixel, nColors, + &colorNumber, &bytesRead); if (bytesRead < charsPerPixel) { if (pixelBytes[bytesRead] == '\0') @@ -778,8 +786,8 @@ getColormapIndex(const char ** const lineCursorP, else { /* colormap shadows ptab[]. Find this color # in ptab[] */ unsigned int i; - for (i = 0; i < ptabSize && ptab[i] != colorNumber; ++i); - if (i < ptabSize) + for (i = 0; i < nColors && ptab[i] != colorNumber; ++i); + if (i < nColors) *colormapIndexP = i; else pm_error("Color number %u is in raster, but not in colormap", -- cgit 1.4.1