about summary refs log tree commit diff
path: root/converter/ppm/pjtoppm.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/ppm/pjtoppm.c')
-rw-r--r--converter/ppm/pjtoppm.c41
1 files changed, 30 insertions, 11 deletions
diff --git a/converter/ppm/pjtoppm.c b/converter/ppm/pjtoppm.c
index cd558855..b0c879a0 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;