about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--converter/ppm/xim.h6
-rw-r--r--converter/ppm/ximtoppm.c39
-rw-r--r--doc/HISTORY7
3 files changed, 20 insertions, 32 deletions
diff --git a/converter/ppm/xim.h b/converter/ppm/xim.h
index ff21203f..116312bb 100644
--- a/converter/ppm/xim.h
+++ b/converter/ppm/xim.h
@@ -63,9 +63,9 @@ typedef struct XimImage {
     short tpics, npics;    /* number of images, total & left in file */
     short ncolors;         /*   "    "  colors in the color table */
     Color* colors;         /* colortable, one byte per r/g/b & pixel */
-    char* author;         /* author credit, copyright, etc */
-    char* date;           /* date image was made, grabbed, etc. */
-    char* program;        /* program used to make this */
+    const char* author;         /* author credit, copyright, etc */
+    const char* date;           /* date image was made, grabbed, etc. */
+    const char* program;        /* program used to make this */
     short ncomments;       /* number of comments strings */
     char** comments;      /* pointers to null terminated strings */
     char* offset;         /* original offset in machine memory */
diff --git a/converter/ppm/ximtoppm.c b/converter/ppm/ximtoppm.c
index 6cd470e9..d844031c 100644
--- a/converter/ppm/ximtoppm.c
+++ b/converter/ppm/ximtoppm.c
@@ -102,6 +102,11 @@ ReadXimHeader(FILE *     const in_fp,
         pm_message("ReadXimHeader: unable to read file header" );
         return(0);
     }
+    /* Force broken ASCIIZ strings to at least be valid ASCIIZ */
+    a_head.author [sizeof(a_head.author)  - 1] = '\0';
+    a_head.date   [sizeof(a_head.date)    - 1] = '\0';
+    a_head.program[sizeof(a_head.program) - 1] = '\0';
+
     if (atoi(a_head.header_size) != sizeof(ImageHeader)) {
         pm_message("ReadXimHeader: header size mismatch" );
         return(0);
@@ -115,35 +120,15 @@ ReadXimHeader(FILE *     const in_fp,
     header->ncolors = atoi(a_head.num_colors);
     header->nchannels = atoi(a_head.num_channels);
     header->bytes_per_line = atoi(a_head.bytes_per_line);
-/*    header->npics = atoi(a_head.num_pictures); */
+#if 0
+    header->npics = atoi(a_head.num_pictures);
+#endif
     header->bits_channel = atoi(a_head.bits_per_channel);
     header->alpha_flag = atoi(a_head.alpha_channel);
-    if (strlen(a_head.author)) {
-        if (!(header->author = calloc((unsigned int)strlen(a_head.author)+1,
-                1))) {
-            pm_message("ReadXimHeader: can't calloc author string" );
-            return(0);
-        }
-        header->width = atoi(a_head.image_width);
-        strncpy(header->author, a_head.author, strlen(a_head.author));
-    }
-    if (strlen(a_head.date)) {
-        if (!(header->date =calloc((unsigned int)strlen(a_head.date)+1,1))){
-            pm_message("ReadXimHeader: can't calloc date string" );
-            return(0);
-        }
-        header->width = atoi(a_head.image_width);
-        strncpy(header->date, a_head.date, strlen(a_head.date));
-    }
-    if (strlen(a_head.program)) {
-        if (!(header->program = calloc(
-                    (unsigned int)strlen(a_head.program) + 1, 1))) {
-            pm_message("ReadXimHeader: can't calloc program string" );
-            return(0);
-        }
-        header->width = atoi(a_head.image_width);
-        strncpy(header->program, a_head.program,strlen(a_head.program));
-    }
+    pm_asprintf(&header->author,  a_head.author);
+    pm_asprintf(&header->date,    a_head.date);
+    pm_asprintf(&header->program, a_head.program);
+
     /* Do double checking for backwards compatibility */
     if (header->npics == 0)
         header->npics = 1;
diff --git a/doc/HISTORY b/doc/HISTORY
index e4e46d0a..303af64a 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -14,6 +14,9 @@ not yet  BJH  Release 10.91.00
               cameratopam: Handle I/O error on input file.  Always broken.
               Cameratopam was new to Netpbm in Netpbm 10.25 (June 2005).
 
+              xbmtopbm: Recognize "unsigned short" as an extension of
+              XBM X10 format.
+
               picttoppm: Fix bug with an input file that specifies a clip
               region (ClipRgn opcode) that is not contained within the
               picture frame.  Result is invalid memory access.  Broken
@@ -30,8 +33,8 @@ not yet  BJH  Release 10.91.00
               pnmtorle: Fix incorrect command and filename in header.
               Broken in Netpbm 10.88 (September 2019).
 
-              xbmtopbm: Recognize "unsigned short" as an extension of
-              XBM X10 format.
+              ximtoppm: Fix possible program crash due to invalid memory
+              reference.
 
 20.03.26 BJH  Release 10.90.00