about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2012-09-21 01:36:26 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2012-09-21 01:36:26 +0000
commit66326f06186f695c9999c1eb1ed43378dd7f63d8 (patch)
tree48a4a3e2dc2aedbf279d9d5d732d62ef51ac9556
parenta729affd6d46ac42c9ac613989a28c359bb8db67 (diff)
downloadnetpbm-mirror-66326f06186f695c9999c1eb1ed43378dd7f63d8.tar.gz
netpbm-mirror-66326f06186f695c9999c1eb1ed43378dd7f63d8.tar.xz
netpbm-mirror-66326f06186f695c9999c1eb1ed43378dd7f63d8.zip
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
-rw-r--r--converter/ppm/xpmtoppm.c20
1 files changed, 14 insertions, 6 deletions
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",