about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--converter/ppm/pjtoppm.c41
-rw-r--r--doc/HISTORY4
2 files changed, 34 insertions, 11 deletions
diff --git a/converter/ppm/pjtoppm.c b/converter/ppm/pjtoppm.c
index 9d23d47b..e556803f 100644
--- a/converter/ppm/pjtoppm.c
+++ b/converter/ppm/pjtoppm.c
@@ -51,7 +51,11 @@ main(int argc, const char ** argv) {
 
     int cmd, val;
     char buffer[BUFSIZ];
-    int planes = 3, rows = -1, cols = -1;
+    int planes = 3;
+    unsigned int rows;
+    unsigned int rowsX;
+    unsigned int cols;
+    bool colsIsSet;
     unsigned char **image = NULL;
     int *imlen;
     FILE * ifP;
@@ -78,6 +82,8 @@ main(int argc, const char ** argv) {
     row = 0;  /* initial value */
     plane = 0;  /* initial value */
     modeIsSet = false;  /* initial value */
+    colsIsSet = false;  /* initial value */
+    rowsX = 0;  /* initial value */
 
     while ((c = fgetc(ifP)) != -1) {
         if (c != '\033')
@@ -119,10 +125,18 @@ main(int argc, const char ** argv) {
             case 'r':
                 switch (c) {
                 case 'S':   /* width */
-                    cols = val;
+                    if (val < 0)
+                        pm_error("invalid width value");
+                    else {
+                        cols = val;
+                        colsIsSet = true;
+                    }
                     break;
                 case 'T':   /* height */
-                    rows = val;
+                    if (val < 0)
+                        pm_error ("invalid height value");
+                    else
+                        rowsX = val;
                     break;
                 case 'U':   /* planes */
                     planes = val;
@@ -153,21 +167,24 @@ main(int argc, const char ** argv) {
                     break;
                 case 'V':   /* send plane */
                 case 'W':   /* send last plane */
-                    if (rows == -1 || row >= rows || image == NULL) {
-                        if (rows == -1 || row >= rows)
-                            rows += 100;
+                    if (row >= rowsX || image == NULL) {
+                        if (row >= rowsX)
+                            rowsX += 100;
                         if (image == NULL) {
-                            MALLOCARRAY(image, uintProduct(rows, planes));
-                            MALLOCARRAY(imlen, uintProduct(rows, planes));
+                            MALLOCARRAY(image, uintProduct(rowsX, planes));
+                            MALLOCARRAY(imlen, uintProduct(rowsX, planes));
                         } else {
-                            REALLOCARRAY(image, uintProduct(rows, planes));
-                            REALLOCARRAY(imlen, uintProduct(rows, planes));
+                            REALLOCARRAY(image, uintProduct(rowsX, planes));
+                            REALLOCARRAY(imlen, uintProduct(rowsX, planes));
                         }
                     }
                     if (image == NULL || imlen == NULL)
                         pm_error("out of memory");
                     if (plane >= planes)
                         pm_error("too many planes");
+                    if (!colsIsSet)
+                        pm_error("missing width value");
+
                     cols = MAX(cols, val);
                     imlen[row * planes + plane] = val;
                     MALLOCARRAY(image[row * planes + plane], val);
@@ -225,7 +242,9 @@ main(int argc, const char ** argv) {
 
     rows = row;
     if (mode == 1) {
-        int const newcols = 10240;  /* It could not be larger that that! */
+        unsigned int const newcols = 10240;
+            /* It could not be larger than that! */
+
         unsigned char * buf;
         unsigned int row;
 
diff --git a/doc/HISTORY b/doc/HISTORY
index ee91e202..02a748ef 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -17,6 +17,10 @@ not yet  BJH  Release 10.91.00
               xbmtopbm: Recognize "unsigned short" as an extension of
               XBM X10 format.
 
+              pjtoppm: Fix handling of input with width unspecified or not
+              positive.  Always broken.  (Pjtoppm was in primordial Netpbm, ca
+              1991).
+
               winicontopam: Fix crash with PNG icon.  Always broken.
               Winicontopam was new in Netpbm 10.63 (June 2013).