[gs-commits] ghostpdl branch, master, updated. jbig2dec-0.14-1986-g15f3fae

[email protected] (Julian Smith) Wed, 27 Nov 2019 18:53:44 +0000 (UTC)
Newsgroups gmane.comp.printing.ghostscript.cvs
Message-ID <[email protected]>
The ghostpdl branch, master has been updated
       via  15f3faeffff7d1b2658b824262314091f7af088b (commit)
       via  6e8c15137a70012776c1b163d7480e1a2cc7c61b (commit)
       via  7e1c4da27805ab9b545bc8ab5b0747c37b69454c (commit)
       via  1df794e69d9f29dae215e50cf328ccad40fe29e8 (commit)
       via  e9ccb4139c5f3fb89a5f4d6554e73323dbf73641 (commit)
       via  e4dcc70672b447df52b0a3c7976ab6ece280841e (commit)
      from  334132f45320ef35b54032c053c100d07e02ec98 (commit)

----------------------------------------------------------------------
commit 15f3faeffff7d1b2658b824262314091f7af088b
Author: Julian Smith <[email protected]>
Date:   Wed Nov 27 15:29:04 2019 +0000

    Coverity 95034: move code so that we have one less call to pcl_cm_is_differential().
    
    fixes scan-build issue, e.g.:
        scan-build -o ../scan-build-out make sanitize

diff --git a/contrib/pcl3/src/gdevpcl3.c b/contrib/pcl3/src/gdevpcl3.c
index 6d2863d..dc6d897 100644
--- a/contrib/pcl3/src/gdevpcl3.c
+++ b/contrib/pcl3/src/gdevpcl3.c
@@ -1493,6 +1493,7 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
     rc = gs_note_error(gs_error_VMerror);
     goto end;
   }
+  eprn_number_of_octets((eprn_Device *)dev, lengths);
   rd.next = (pcl_OctetString *)malloc(planes*sizeof(pcl_OctetString));
   if (!rd.next) {
     rc = gs_note_error(gs_error_VMerror);
@@ -1510,8 +1511,14 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
     for (j=0; j<planes; j++) { /* Make sure we can free at any point. */
       rd.previous[j].str = NULL;
     }
+    for (j = 0; j < planes; j++) {
+      rd.previous[j].str = (pcl_Octet *)malloc(lengths[j]*sizeof(eprn_Octet));
+      if (!rd.previous[j].str) {
+        rc = gs_note_error(gs_error_VMerror);
+        goto end;
+      }
+    }
   }
-  eprn_number_of_octets((eprn_Device *)dev, lengths);
   rd.width = 8*lengths[0];      /* all colorants have equal resolution */
   for (j = 0; j < planes; j++) {
     rd.next[j].str = (pcl_Octet *)malloc(lengths[j]*sizeof(eprn_Octet));
@@ -1521,14 +1528,6 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
     }
   }
     /* Note: 'pcl_Octet' must be identical with 'eprn_Octet'. */
-  if (pcl_cm_is_differential(dev->file_data.compression))
-    for (j = 0; j < planes; j++) {
-      rd.previous[j].str = (pcl_Octet *)malloc(lengths[j]*sizeof(eprn_Octet));
-      if (!rd.previous[j].str) {
-        rc = gs_note_error(gs_error_VMerror);
-        goto end;
-      }
-    }
   rd.workspace_allocated = lengths[0];
   for (j = 1; j < planes; j++)
     if (lengths[j] > rd.workspace_allocated)

----------------------------------------------------------------------
commit 6e8c15137a70012776c1b163d7480e1a2cc7c61b
Author: Julian Smith <[email protected]>
Date:   Wed Nov 27 15:27:25 2019 +0000

    Coverity 95034: removed use of guard() macro.
    
    Instead use static fn to convert error codes, with explicit inline code.

diff --git a/contrib/pcl3/src/gdevpcl3.c b/contrib/pcl3/src/gdevpcl3.c
index 585e774..6d2863d 100644
--- a/contrib/pcl3/src/gdevpcl3.c
+++ b/contrib/pcl3/src/gdevpcl3.c
@@ -1443,17 +1443,13 @@ static int pcl3_close_device(gx_device *device)
 
 ******************************************************************************/
 
-/* Macro to handle return codes from calls to pclgen routines */
-#define guard(call) \
-    if ((rc = (call)) != 0) { \
-      if (rc > 0) { \
-        rc = gs_note_error(gs_error_Fatal); /* bugs are fatal :-) */ \
-      } \
-      else { \
-        rc = gs_note_error(gs_error_ioerror);   /* actually any environment error */ \
-      } \
-      goto end; \
-    }
+/* Function to convert return codes from calls to pclgen routines. */
+static int convert(int code)
+{
+    if (code > 0)   return gs_error_Fatal;
+    if (code < 0)   return gs_error_ioerror;
+    return 0;
+}
 
 static int pcl3_print_page(gx_device_printer *device, gp_file *out)
 {
@@ -1481,7 +1477,11 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
      printer first */
   if (gdev_prn_file_is_new(device) || !dev->configured ||
       dev->configure_every_page) {
-    guard(pcl3_init_file(device->memory, out, &dev->file_data))
+    rc = convert(pcl3_init_file(device->memory, out, &dev->file_data));
+    if (rc) {
+        gs_note_error(rc);
+        goto end;
+    }
     dev->configured = true;
   }
 
@@ -1507,7 +1507,7 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
       rc = gs_note_error(gs_error_VMerror);
       goto end;
     }
-    for (j=0; j<planes; j++) {
+    for (j=0; j<planes; j++) { /* Make sure we can free at any point. */
       rd.previous[j].str = NULL;
     }
   }
@@ -1544,8 +1544,16 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
   }
 
   /* Open the page and start raster mode */
-  guard(pcl3_begin_page(out, &dev->file_data))
-  guard(pcl3_begin_raster(out, &rd))
+  rc = convert(pcl3_begin_page(out, &dev->file_data));
+  if (rc) {
+    gs_note_error(rc);
+    goto end;
+  }
+  rc = convert(pcl3_begin_raster(out, &rd));
+  if (rc) {
+    gs_note_error(rc);
+    goto end;
+  }
 
   /* Loop over scan lines */
   blank_lines = 0;
@@ -1579,16 +1587,32 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
     if (j == planes) blank_lines++;
     else {
       if (blank_lines > 0) {
-        guard(pcl3_skip_groups(out, &rd, blank_lines))
+        rc = convert(pcl3_skip_groups(out, &rd, blank_lines));
+        if (rc) {
+          gs_note_error(rc);
+          goto end;
+        }
         blank_lines = 0;
       }
-      guard(pcl3_transfer_group(out, &rd))
+      rc = convert(pcl3_transfer_group(out, &rd));
+      if (rc) {
+        gs_note_error(rc);
+        goto end;
+      }
     }
   }
 
   /* Terminate raster mode and close the page */
-  guard(pcl3_end_raster(out, &rd))
-  guard(pcl3_end_page(out, &dev->file_data))
+  rc = convert(pcl3_end_raster(out, &rd));
+  if (rc) {
+    gs_note_error(rc);
+    goto end;
+  }
+  rc = convert(pcl3_end_page(out, &dev->file_data));
+  if (rc) {
+    gs_note_error(rc);
+    goto end;
+  }
 
 end:
   /* Free dynamic storage */
@@ -1616,5 +1640,3 @@ end:
 
   return rc;
 }
-
-#undef guard

----------------------------------------------------------------------
commit 7e1c4da27805ab9b545bc8ab5b0747c37b69454c
Author: Julian Smith <[email protected]>
Date:   Wed Nov 27 14:29:41 2019 +0000

    Coverity 95034: refactor pcl3_print_page() to avoid leaks.

diff --git a/contrib/pcl3/src/gdevpcl3.c b/contrib/pcl3/src/gdevpcl3.c
index 4a63788..585e774 100644
--- a/contrib/pcl3/src/gdevpcl3.c
+++ b/contrib/pcl3/src/gdevpcl3.c
@@ -1444,17 +1444,22 @@ static int pcl3_close_device(gx_device *device)
 ******************************************************************************/
 
 /* Macro to handle return codes from calls to pclgen routines */
-#define guard(call)                                                     \
-    if ((rc = (call)) != 0) {                                           \
-      if (rc > 0) return_error(gs_error_Fatal); /* bugs are fatal :-) */ \
-      return_error(gs_error_ioerror);   /* actually any environment error */ \
+#define guard(call) \
+    if ((rc = (call)) != 0) { \
+      if (rc > 0) { \
+        rc = gs_note_error(gs_error_Fatal); /* bugs are fatal :-) */ \
+      } \
+      else { \
+        rc = gs_note_error(gs_error_ioerror);   /* actually any environment error */ \
+      } \
+      goto end; \
     }
 
 static int pcl3_print_page(gx_device_printer *device, gp_file *out)
 {
   int
     blank_lines,
-    rc;
+    rc = 0;
   pcl3_Device *dev = (pcl3_Device *)device;
   const char *epref = dev->eprn.CUPS_messages? CUPS_ERRPREF: "";
   pcl_RasterData rd;
@@ -1463,6 +1468,15 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
     *lengths,
     planes;
 
+  /* Make sure out cleanup code will cope with partially-initialised data. */
+  memset(&rd, 0, sizeof(pcl_RasterData)); /* Belt and braces. */
+  planes = 0;
+  lengths = NULL;
+  rd.next = NULL;
+  rd.previous = NULL;
+  rd.workspace[0] = NULL;
+  rd.workspace[1] = NULL;
+
   /* If this is a new file or we've decided to re-configure, initialize the
      printer first */
   if (gdev_prn_file_is_new(device) || !dev->configured ||
@@ -1472,61 +1486,61 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
   }
 
   /* Initialize raster data structure */
-  memset(&rd, 0, sizeof(pcl_RasterData));
   rd.global = &dev->file_data;
   planes = eprn_number_of_bitplanes((eprn_Device *)dev);
   lengths = (unsigned int *)malloc(planes*sizeof(unsigned int));
+  if (!lengths) {
+    rc = gs_note_error(gs_error_VMerror);
+    goto end;
+  }
   rd.next = (pcl_OctetString *)malloc(planes*sizeof(pcl_OctetString));
-  if (pcl_cm_is_differential(dev->file_data.compression))
+  if (!rd.next) {
+    rc = gs_note_error(gs_error_VMerror);
+    goto end;
+  }
+  for (j=0; j<planes; j++) { /* Make sure we can free at any point. */
+    rd.next[j].str = NULL;
+  }
+  if (pcl_cm_is_differential(dev->file_data.compression)) {
     rd.previous = (pcl_OctetString *)malloc(planes*sizeof(pcl_OctetString));
-  if (lengths == NULL || rd.next == NULL ||
-      (pcl_cm_is_differential(dev->file_data.compression) &&
-        rd.previous == NULL)) {
-    free(lengths); free(rd.next); free(rd.previous);
-    eprintf1("%s" ERRPREF "Memory allocation failure from malloc().\n",
-      epref);
-    return_error(gs_error_VMerror);
+    if (!rd.previous) {
+      rc = gs_note_error(gs_error_VMerror);
+      goto end;
+    }
+    for (j=0; j<planes; j++) {
+      rd.previous[j].str = NULL;
+    }
   }
   eprn_number_of_octets((eprn_Device *)dev, lengths);
   rd.width = 8*lengths[0];      /* all colorants have equal resolution */
-  for (j = 0; j < planes; j++)
+  for (j = 0; j < planes; j++) {
     rd.next[j].str = (pcl_Octet *)malloc(lengths[j]*sizeof(eprn_Octet));
+    if (!rd.next[j].str) {
+      rc = gs_note_error(gs_error_VMerror);
+      goto end;
+    }
+  }
     /* Note: 'pcl_Octet' must be identical with 'eprn_Octet'. */
   if (pcl_cm_is_differential(dev->file_data.compression))
-    for (j = 0; j < planes; j++)
+    for (j = 0; j < planes; j++) {
       rd.previous[j].str = (pcl_Octet *)malloc(lengths[j]*sizeof(eprn_Octet));
+      if (!rd.previous[j].str) {
+        rc = gs_note_error(gs_error_VMerror);
+        goto end;
+      }
+    }
   rd.workspace_allocated = lengths[0];
   for (j = 1; j < planes; j++)
     if (lengths[j] > rd.workspace_allocated)
       rd.workspace_allocated = lengths[j];
   for (j = 0;
-      j < 2 && (j != 1 || dev->file_data.compression == pcl_cm_delta); j++)
+      j < 2 && (j != 1 || dev->file_data.compression == pcl_cm_delta); j++) {
     rd.workspace[j] =
       (pcl_Octet *)malloc(rd.workspace_allocated*sizeof(pcl_Octet));
-
-  /* Collective check for allocation failures */
-  j = 0;
-  while (j < planes && rd.next[j].str != NULL) j++;
-  if (j == planes && pcl_cm_is_differential(dev->file_data.compression)) {
-    j = 0;
-    while (j < planes && rd.previous[j].str != NULL) j++;
-    if (j == planes && dev->file_data.compression == pcl_cm_delta &&
-        rd.workspace[1] == NULL) j = 0;
-  }
-  if (j < planes || rd.workspace[0] == NULL) {
-    /* Free everything. Note that free(NULL) is legal and we did a memset()
-       with 0 on 'rd'. */
-    for (j = 0; j < planes; j++) {
-      free(rd.next[j].str);
-      if (pcl_cm_is_differential(dev->file_data.compression))
-        free(rd.previous[j].str);
+    if (!rd.workspace[j]) {
+      rc = gs_note_error(gs_error_VMerror);
+      goto end;
     }
-    free(lengths); free(rd.next); free(rd.previous);
-    for (j = 0; j < 2; j++) free(rd.workspace[j]);
-
-    eprintf1("%s" ERRPREF "Memory allocation failure from malloc().\n",
-      epref);
-    return_error(gs_error_VMerror);
   }
 
   /* Open the page and start raster mode */
@@ -1576,14 +1590,31 @@ static int pcl3_print_page(gx_device_printer *device, gp_file *out)
   guard(pcl3_end_raster(out, &rd))
   guard(pcl3_end_page(out, &dev->file_data))
 
+end:
   /* Free dynamic storage */
-  for (j = 0; j < planes; j++) free(rd.next[j].str);
-  if (pcl_cm_is_differential(dev->file_data.compression))
-    for (j = 0; j < planes; j++) free(rd.previous[j].str);
-  for (j = 0; j < 2; j++) free(rd.workspace[j]);
-  free(lengths); free(rd.next); free(rd.previous);
+  if (rd.next) {
+    for (j = 0; j < planes; j++) {
+      free(rd.next[j].str);
+    }
+  }
+  if (rd.previous) {
+    for (j = 0; j < planes; j++) {
+      free(rd.previous[j].str);
+    }
+  }
+  for (j = 0; j < 2; j++) {
+    free(rd.workspace[j]);
+  }
+  free(lengths);
+  free(rd.next);
+  free(rd.previous);
 
-  return 0;
+  if (rc == gs_error_VMerror) {
+    eprintf1("%s" ERRPREF "Memory allocation failure from malloc().\n",
+      epref);
+  }
+
+  return rc;
 }
 
 #undef guard

----------------------------------------------------------------------
commit 1df794e69d9f29dae215e50cf328ccad40fe29e8
Author: Julian Smith <[email protected]>
Date:   Wed Nov 27 13:11:29 2019 +0000

    Coverity 95027: fix off-by-one when bounds-checking against LIPS_MEDIACHAR_MAX.

diff --git a/contrib/lips4/gdevl4r.c b/contrib/lips4/gdevl4r.c
index 05f1f81..054f5ea 100644
--- a/contrib/lips4/gdevl4r.c
+++ b/contrib/lips4/gdevl4r.c
@@ -502,7 +502,7 @@ lips4_put_params(gx_device * pdev, gs_param_list * plist)
                                      (param_name = LIPS_OPTION_MEDIATYPE),
                                      &pmedia)) {
         case 0:
-            if (pmedia.size > LIPS_MEDIACHAR_MAX) {
+            if (pmedia.size >= LIPS_MEDIACHAR_MAX) {
                 ecode = gs_error_limitcheck;
                 goto pmediae;
             } else {   /* Check the validity of ``MediaType'' characters */

----------------------------------------------------------------------
commit e9ccb4139c5f3fb89a5f4d6554e73323dbf73641
Author: Julian Smith <[email protected]>
Date:   Wed Nov 27 12:52:17 2019 +0000

    Coverity 94957: Call dlclose() to avoid leaks of handles from dlopen().

diff --git a/contrib/opvp/gdevopvp.c b/contrib/opvp/gdevopvp.c
index cb2921e..b49d507 100644
--- a/contrib/opvp/gdevopvp.c
+++ b/contrib/opvp/gdevopvp.c
@@ -1796,6 +1796,7 @@ opvp_load_vector_driver(void)
                 }
                 OpenPrinter_0_2 = NULL;
                 ErrorNo = NULL;
+                dlclose(h);
             }
             i++;
         }

----------------------------------------------------------------------
commit e4dcc70672b447df52b0a3c7976ab6ece280841e
Author: Julian Smith <[email protected]>
Date:   Wed Nov 27 11:39:28 2019 +0000

    Coverity 350199: mark cf_encode_1d() as not tainting lbuf.
    
    This may work better than the previous annotation.

diff --git a/base/scfe.c b/base/scfe.c
index 182cfe5..9ed4a07 100644
--- a/base/scfe.c
+++ b/base/scfe.c
@@ -381,7 +381,7 @@ s_CFE_process(stream_state * st, stream_cursor_read * pr,
 
 /* Encode a 1-D scan line. */
 /* Attempt to stop coverity thinking skip_white_pixels() taints lbuf:*/
-/* coverity [ -tainted_scalar ] */
+/* coverity [ -tainted_data_argument arg-1 ] */
 static void
 cf_encode_1d(stream_CFE_state * ss, const byte * lbuf, stream_cursor_write * pw)
 {


Summary of changes:
 base/scfe.c                 |   2 +-
 contrib/lips4/gdevl4r.c     |   2 +-
 contrib/opvp/gdevopvp.c     |   1 +
 contrib/pcl3/src/gdevpcl3.c | 172 ++++++++++++++++++++++++++++----------------
 4 files changed, 115 insertions(+), 62 deletions(-)