Blame SOURCES/ghostscript-hanging-in-convert.patch

abc3ef
From 85cebb9760339a7853f6eef6be03812f24492f40 Mon Sep 17 00:00:00 2001
abc3ef
From: Michael Vrhel <michael.vrhel@artifex.com>
abc3ef
Date: Sat, 8 Jun 2013 09:12:30 -0700
abc3ef
Subject: [PATCH 1/5] Change in behavior for overprint. Fixes bugs 694295
abc3ef
 694296 and 694067
abc3ef
abc3ef
With this change we now do simulated overprinting of cmyk AND spot colorants
abc3ef
for contone cmyk devices by default.   If -dSimulateOverprint=false is specified we
abc3ef
will not simulate overprinting of any colors.
abc3ef
abc3ef
Also, cmyk simulation of spot colors with separation devices is not handled.
abc3ef
i.e. if you specify -dSimulateOverprint=true -dMaxSpots=0  -sDEVICE=tiffsep1
abc3ef
you will see the spot colors blended in CMYK space if overprint has been
abc3ef
specified in the document.
abc3ef
abc3ef
Note that not all overprint situations can be simulated accurately with this
abc3ef
approach.  For example, if I have a spot color that resulted in a CMYK value
abc3ef
of 0 0 100 0 after the alternate tint transform and then I did an overprint with
abc3ef
a CMYK value of 0 0 1 0, the color should still be a bright yellow but will actually
abc3ef
be almost no color.   The issue is that we can not distinguish this case from one
abc3ef
where we had first laid down a CMYK of 0 0 100 0 followed by the overprint of 0 0 1 0
abc3ef
which should be a light yellow.  The documentation has been updated to point this out.
abc3ef
abc3ef
I reviewed all the diffs that came up in the bmpcmp and reviewed the Ghent overprint
abc3ef
files.
abc3ef
---
abc3ef
 base/gscdevn.c      | 10 +++++++---
abc3ef
 base/gscsepr.c      |  8 ++++++--
abc3ef
 base/gsdparam.c     |  4 ++--
abc3ef
 base/gsicc.c        |  7 +++++--
abc3ef
 base/gsicc_manage.c |  2 +-
abc3ef
 base/gsovrc.c       | 43 ++++++++++++++++++++++++-------------------
abc3ef
 base/gsstate.c      | 13 ++++++++++++-
abc3ef
 base/lib.mak        |  4 ++--
abc3ef
 doc/Use.htm         | 11 +++++++++--
abc3ef
 9 files changed, 68 insertions(+), 34 deletions(-)
abc3ef
abc3ef
diff --git a/base/gscdevn.c b/base/gscdevn.c
abc3ef
index 4963d14..348d543 100644
abc3ef
--- a/base/gscdevn.c
abc3ef
+++ b/base/gscdevn.c
abc3ef
@@ -40,6 +40,7 @@
abc3ef
 #include "gsicc_manage.h"
abc3ef
 #include "gsicc.h"
abc3ef
 #include "gsicc_cache.h"
abc3ef
+#include "gxdevice.h"
abc3ef
 
abc3ef
 /* ---------------- Color space ---------------- */
abc3ef
 
abc3ef
@@ -688,14 +689,16 @@ gx_set_overprint_DeviceN(const gs_color_space * pcs, gs_state * pgs)
abc3ef
     if (pcmap->use_alt_cspace) {
abc3ef
         const gs_color_space_type* base_type = pcs->base_space->type;
abc3ef
 
abc3ef
-        if (dev_profile->sim_overprint)
abc3ef
+        if (dev_profile->sim_overprint &&
abc3ef
+            dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE &&
abc3ef
+            !gx_device_must_halftone(dev))
abc3ef
             return gx_simulated_set_overprint(pcs->base_space, pgs);
abc3ef
         else {
abc3ef
             /* If the base space is DeviceCMYK, handle overprint as DeviceCMYK */
abc3ef
             if ( base_type->index == gs_color_space_index_DeviceCMYK )
abc3ef
-                    return base_type->set_overprint( pcs->base_space, pgs );
abc3ef
+                return base_type->set_overprint( pcs->base_space, pgs );
abc3ef
             else
abc3ef
-                    return gx_spot_colors_set_overprint( pcs->base_space, pgs);
abc3ef
+                return gx_spot_colors_set_overprint( pcs->base_space, pgs);
abc3ef
         }
abc3ef
     }
abc3ef
     else {
abc3ef
@@ -707,6 +710,7 @@ gx_set_overprint_DeviceN(const gs_color_space * pcs, gs_state * pgs)
abc3ef
             params.retain_spot_comps = false;
abc3ef
             params.drawn_comps = 0;
abc3ef
             params.k_value = 0;
abc3ef
+            /* We should not have to blend if we don't need the alternate tint transform */
abc3ef
             params.blendspot = false;
abc3ef
             for (i = 0; i < ncomps; i++) {
abc3ef
                 int     mcomp = pcmap->color_map[i];
abc3ef
diff --git a/base/gscsepr.c b/base/gscsepr.c
abc3ef
index 4f477f7..c9b20c5 100644
abc3ef
--- a/base/gscsepr.c
abc3ef
+++ b/base/gscsepr.c
abc3ef
@@ -33,6 +33,7 @@
abc3ef
 #include "gsovrc.h"
abc3ef
 #include "stream.h"
abc3ef
 #include "gsicc_cache.h"
abc3ef
+#include "gxdevice.h"
abc3ef
 
abc3ef
 /* ---------------- Color space ---------------- */
abc3ef
 
abc3ef
@@ -149,20 +150,23 @@ gx_set_overprint_Separation(const gs_color_space * pcs, gs_state * pgs)
abc3ef
     
abc3ef
     dev_proc(dev, get_profile)(dev, &(dev_profile));
abc3ef
     if (pcmap->use_alt_cspace)
abc3ef
-        if (dev_profile->sim_overprint)
abc3ef
+        if (dev_profile->sim_overprint && 
abc3ef
+            dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE &&
abc3ef
+            !gx_device_must_halftone(dev))
abc3ef
             return gx_simulated_set_overprint(pcs->base_space, pgs);
abc3ef
         else
abc3ef
             return gx_spot_colors_set_overprint(pcs->base_space, pgs);
abc3ef
     else {
abc3ef
         gs_overprint_params_t   params;
abc3ef
 
abc3ef
+        /* We should not have to blend if we don't need the alternate tint transform */
abc3ef
+        params.blendspot = false;
abc3ef
         params.retain_any_comps = pgs->overprint &&
abc3ef
                                   pcs->params.separation.sep_type != SEP_ALL;
abc3ef
         if (params.retain_any_comps) {
abc3ef
             params.retain_spot_comps = false;
abc3ef
             params.drawn_comps = 0;
abc3ef
             params.k_value = 0;
abc3ef
-            params.blendspot = false;
abc3ef
             if (pcs->params.separation.sep_type != SEP_NONE) {
abc3ef
                 int     mcomp = pcmap->color_map[0];
abc3ef
 
abc3ef
diff --git a/base/gsdparam.c b/base/gsdparam.c
abc3ef
index 030daec..e2a3e54 100644
abc3ef
--- a/base/gsdparam.c
abc3ef
+++ b/base/gsdparam.c
abc3ef
@@ -80,7 +80,7 @@ gx_default_get_params(gx_device * dev, gs_param_list * plist)
abc3ef
     gsicc_blackpreserve_t blackpreserve[NUM_DEVICE_PROFILES];
abc3ef
     bool devicegraytok = true;  /* Default if device profile stuct not set */
abc3ef
     bool usefastcolor = false;  /* set for unmanaged color */
abc3ef
-    bool sim_overprint = false;
abc3ef
+    bool sim_overprint = true;  /* By default simulate overprinting */
abc3ef
     bool prebandthreshold = true;
abc3ef
     int k;
abc3ef
     gs_param_float_array msa, ibba, hwra, ma;
abc3ef
@@ -803,7 +803,7 @@ gx_default_put_params(gx_device * dev, gs_param_list * plist)
abc3ef
     int k;
abc3ef
     bool devicegraytok = true;
abc3ef
     bool usefastcolor = false;
abc3ef
-    bool sim_overprint = false;
abc3ef
+    bool sim_overprint = true;
abc3ef
     bool prebandthreshold = false;
abc3ef
     int  profile_types[NUM_DEVICE_PROFILES] = {gsDEFAULTPROFILE,
abc3ef
                                                gsGRAPHICPROFILE,
abc3ef
diff --git a/base/gsicc.c b/base/gsicc.c
abc3ef
index 80380b7..e7093c5 100644
abc3ef
--- a/base/gsicc.c
abc3ef
+++ b/base/gsicc.c
abc3ef
@@ -608,14 +608,17 @@ gx_set_overprint_ICC(const gs_color_space * pcs, gs_state * pgs)
abc3ef
 {
abc3ef
     gx_device *             dev = pgs->device;
abc3ef
     gx_device_color_info *  pcinfo = (dev == 0 ? 0 : &dev->color_info);
abc3ef
+    cmm_dev_profile_t *dev_profile;
abc3ef
+    int code = dev_proc(dev, get_profile)(dev, &dev_profile);
abc3ef
 
abc3ef
     /* check if we require special handling */
abc3ef
     if ( !pgs->overprint                      ||
abc3ef
          pgs->overprint_mode != 1             ||
abc3ef
          pcinfo == 0                          ||
abc3ef
          pcs->cmm_icc_profile_data->data_cs != gsCMYK ||
abc3ef
-         pcinfo->opmode == GX_CINFO_OPMODE_NOT  )
abc3ef
-        return gx_spot_colors_set_overprint(pcs, pgs);
abc3ef
+         pcinfo->opmode == GX_CINFO_OPMODE_NOT  ) {
abc3ef
+            return gx_spot_colors_set_overprint(pcs, pgs);
abc3ef
+    }
abc3ef
 
abc3ef
     if (pcinfo->opmode == GX_CINFO_OPMODE_RGB || 
abc3ef
         pcinfo->opmode == GC_CINFO_OPMODE_RGB_SET) {
abc3ef
diff --git a/base/gsicc_manage.c b/base/gsicc_manage.c
abc3ef
index 05e618b..03cc29c 100644
abc3ef
--- a/base/gsicc_manage.c
abc3ef
+++ b/base/gsicc_manage.c
abc3ef
@@ -1179,7 +1179,7 @@ gsicc_new_device_profile_array(gs_memory_t *memory)
abc3ef
     result->usefastcolor = false;  /* Default is to not use fast color */
abc3ef
     result->prebandthreshold = true;
abc3ef
     result->supports_devn = false;
abc3ef
-    result->sim_overprint = false;
abc3ef
+    result->sim_overprint = true;
abc3ef
     rc_init_free(result, memory->non_gc_memory, 1, rc_free_profile_array);
abc3ef
     return(result);
abc3ef
 }
abc3ef
diff --git a/base/gsovrc.c b/base/gsovrc.c
abc3ef
index 890b2f7..3b741b9 100644
abc3ef
--- a/base/gsovrc.c
abc3ef
+++ b/base/gsovrc.c
abc3ef
@@ -125,6 +125,7 @@ c_overprint_equal(const gs_composite_t * pct0, const gs_composite_t * pct1)
abc3ef
  */
abc3ef
 #define OVERPRINT_ANY_COMPS     1
abc3ef
 #define OVERPRINT_SPOT_COMPS    2
abc3ef
+#define OVERPRINT_BLEND         4
abc3ef
 
abc3ef
 /*
abc3ef
  * Convert an overprint compositor to string form for use by the command
abc3ef
@@ -140,15 +141,15 @@ c_overprint_write(const gs_composite_t * pct, byte * data, uint * psize, gx_devi
abc3ef
     /* encoded the booleans in a single byte */
abc3ef
     if (pparams->retain_any_comps) {
abc3ef
         flags |= OVERPRINT_ANY_COMPS;
abc3ef
-
abc3ef
-        /* write out the component bits only if necessary (and possible) */
abc3ef
-        if (pparams->retain_spot_comps && !pparams->blendspot)
abc3ef
+        if (pparams->blendspot)
abc3ef
+            flags |= OVERPRINT_BLEND;
abc3ef
+        if (pparams->retain_spot_comps)
abc3ef
             flags |= OVERPRINT_SPOT_COMPS;
abc3ef
-        else {
abc3ef
-            uint    tmp_size = (avail > 0 ? avail - 1 : 0);
abc3ef
-            int     code = write_color_index( pparams->drawn_comps,
abc3ef
-                                              data + 1,
abc3ef
-                                              &tmp_size );
abc3ef
+        /* write out the component bits only if necessary (and possible) */
abc3ef
+        if (!pparams->retain_spot_comps || pparams->blendspot) {
abc3ef
+            uint tmp_size = (avail > 0 ? avail - 1 : 0);
abc3ef
+            int code = write_color_index(pparams->drawn_comps, data + 1, 
abc3ef
+                                             &tmp_size);
abc3ef
             /* It would be nice to do have an If RGB OP case, then write out 
abc3ef
                K value, but on the reader side, there is no way to find this
abc3ef
                out so we will always write it out if we are writing the
abc3ef
@@ -199,11 +200,11 @@ c_overprint_read(
abc3ef
     params.retain_any_comps = (flags & OVERPRINT_ANY_COMPS) != 0;
abc3ef
     params.retain_spot_comps = (flags & OVERPRINT_SPOT_COMPS) != 0;
abc3ef
     params.idle = 0;
abc3ef
-    params.blendspot = false;
abc3ef
+    params.blendspot = (flags & OVERPRINT_BLEND) != 0;
abc3ef
     params.k_value = 0;
abc3ef
 
abc3ef
     /* check if the drawn_comps array is present */
abc3ef
-    if (params.retain_any_comps && !params.retain_spot_comps) {
abc3ef
+    if (params.retain_any_comps && (!params.retain_spot_comps || params.blendspot)) {
abc3ef
         code = read_color_index(&params.drawn_comps, data + 1, size - 1);
abc3ef
         if (code < 0)
abc3ef
             return code;
abc3ef
@@ -1181,21 +1182,25 @@ overprint_fill_rectangle_hl_color(gx_device *dev,
abc3ef
                 return code;
abc3ef
             }
abc3ef
             if (blendspot) {
abc3ef
-                /* Skip the plane if this component is not to be drawn.  We have
abc3ef
-                   to do a get bits for each plane due to the fact that we have
abc3ef
-                   to do a copy_planes at the end.  If we had a copy_plane 
abc3ef
-                   operation we would just get the ones need and set those. */
abc3ef
-                if ((comps & 0x01) == 1) {
abc3ef
-                    /* Not sure if a loop or a memset is better here */
abc3ef
-                    memset(gb_params.data[k], 
abc3ef
-                           ((pdcolor->colors.devn.values[k]) >> shift & mask), w);
abc3ef
+                /* We need to blend the CMYK colorants as we are simulating
abc3ef
+                   the overprint of a spot colorant with its equivalent CMYK
abc3ef
+                   colorants */
abc3ef
+                if ((comps &  0x01) == 1) {
abc3ef
+                    int kk;
abc3ef
+                    byte *cp = gb_params.data[k];
abc3ef
+                    byte new_val = ((pdcolor->colors.devn.values[k]) >> shift & mask);
abc3ef
+                    for (kk = 0; kk < w; kk++, cp++) {
abc3ef
+                        int temp = (255 - *cp) * (255 - new_val);
abc3ef
+                        temp = temp >> 8;
abc3ef
+                        *cp = (255-temp);
abc3ef
+                    }
abc3ef
                 }
abc3ef
                 comps >>= 1;
abc3ef
             } else {
abc3ef
                 /* Skip the plane if this component is not to be drawn.  We have
abc3ef
                    to do a get bits for each plane due to the fact that we have
abc3ef
                    to do a copy_planes at the end.  If we had a copy_plane 
abc3ef
-                   operation we would just get the ones need and set those. */
abc3ef
+                   operation we would just get the ones needed and set those. */
abc3ef
                 if ((comps & 0x01) == 1) {
abc3ef
                     /* Not sure if a loop or a memset is better here */
abc3ef
                     memset(gb_params.data[k], 
abc3ef
diff --git a/base/gsstate.c b/base/gsstate.c
abc3ef
index 516fc70..e3e5b70 100644
abc3ef
--- a/base/gsstate.c
abc3ef
+++ b/base/gsstate.c
abc3ef
@@ -675,7 +675,18 @@ void
abc3ef
 gs_setoverprint(gs_state * pgs, bool ovp)
abc3ef
 {
abc3ef
     bool    prior_ovp = pgs->overprint;
abc3ef
-
abc3ef
+    cmm_dev_profile_t *profile_struct;
abc3ef
+    gx_device *dev = pgs->device;
abc3ef
+
abc3ef
+    /* Check if overprint is disabled */
abc3ef
+    if (dev != NULL) {
abc3ef
+        if (dev->procs.get_profile == NULL) {
abc3ef
+            profile_struct = dev->icc_struct;
abc3ef
+        } else {
abc3ef
+            dev_proc(dev, get_profile)(dev,  &profile_struct);
abc3ef
+        }
abc3ef
+        if (profile_struct->sim_overprint == false) return;
abc3ef
+    }
abc3ef
     pgs->overprint = ovp;
abc3ef
     if (prior_ovp != ovp)
abc3ef
         (void)gs_do_set_overprint(pgs);
abc3ef
diff --git a/base/lib.mak b/base/lib.mak
abc3ef
index 10d0d0d..59e0f5c 100644
abc3ef
--- a/base/lib.mak
abc3ef
+++ b/base/lib.mak
abc3ef
@@ -2898,7 +2898,7 @@ $(GLD)seprlib.dev : $(LIB_MAK) $(ECHOGS_XE) $(seprlib_)
abc3ef
 $(GLOBJ)gscsepr.$(OBJ) : $(GLSRC)gscsepr.c $(AK) $(gx_h) $(gserrors_h)\
abc3ef
  $(memory__h) $(gsfunc_h) $(gsrefct_h) $(gsmatrix_h) $(gscsepr_h) $(gxcspace_h)\
abc3ef
  $(gxfixed_h) $(gxcolor2_h) $(gzstate_h) $(gscdevn_h) $(gxcdevn_h)\
abc3ef
- $(gxcmap_h) $(gxdevcli_h) $(gsovrc_h) $(stream_h) $(gsicc_cache_h)\
abc3ef
+ $(gxcmap_h) $(gxdevcli_h) $(gsovrc_h) $(stream_h) $(gsicc_cache_h) $(gxdevice_h)\
abc3ef
  $(MAKEDIRS)
abc3ef
 	$(GLCC) $(GLO_)gscsepr.$(OBJ) $(C_) $(GLSRC)gscsepr.c
abc3ef
 
abc3ef
@@ -2972,7 +2972,7 @@ $(GLOBJ)gscdevn.$(OBJ) : $(GLSRC)gscdevn.c $(AK) $(gx_h) $(gserrors_h)\
abc3ef
  $(gscdevn_h) $(gsfunc_h) $(gsmatrix_h) $(gsrefct_h) $(gsstruct_h)\
abc3ef
  $(gxcspace_h) $(gxcdevn_h) $(gxfarith_h) $(gxfrac_h) $(gsnamecl_h) $(gxcmap_h)\
abc3ef
  $(gxistate_h) $(gscoord_h) $(gzstate_h) $(gxdevcli_h) $(gsovrc_h) $(stream_h)\
abc3ef
- $(gsicc_manage_h) $(gsicc_cache_h) $(MAKEDIRS)
abc3ef
+ $(gsicc_manage_h) $(gsicc_cache_h) $(gxdevice_h) $(MAKEDIRS)
abc3ef
 	$(GLCC) $(GLO_)gscdevn.$(OBJ) $(C_) $(GLSRC)gscdevn.c
abc3ef
 
abc3ef
 $(GLOBJ)gxdevndi.$(OBJ) : $(GLSRC)gxdevndi.c $(AK) $(gx_h)\
abc3ef
diff --git a/doc/Use.htm b/doc/Use.htm
abc3ef
index 2bbc2f0..133b2ec 100644
abc3ef
--- a/doc/Use.htm
abc3ef
+++ b/doc/Use.htm
abc3ef
@@ -3057,8 +3057,15 @@ removal mappings.
abc3ef
 
abc3ef
 
-dSimulateOverprint=true/false
abc3ef
 
abc3ef
-This option enables CMYK devices (e.g. tiff32nc) to provide an simulation of
abc3ef
-overprinting. 
abc3ef
+This option enables continous tone CMYK devices (e.g. tiff32nc) the capability to
abc3ef
+provide a  simulation of spot color overprinting.  The default setting is true.  
abc3ef
+Note that not all spot color overprint cases can be accurately simulated with a CMYK 
abc3ef
+only device.   For example, a case where you have a spot color overprinted with CMYK
abc3ef
+colors will be indistiguishable from a case where you have spot color equivalent 
abc3ef
+CMYK colorants overprinted with CMYK colors, even though they may need to show 
abc3ef
+significantly different overprint simulations.  To obtain a full overprint simulation,
abc3ef
+use the psdcmyk or tiffsep device, where the spot colors are kept in their own 
abc3ef
+individual planes.
abc3ef
 
abc3ef
 
abc3ef
 
abc3ef
-- 
abc3ef
2.5.5
abc3ef
abc3ef
abc3ef
From 27c1c6e80b0d4c62d9ced7bf986a7bdb7b444d67 Mon Sep 17 00:00:00 2001
abc3ef
From: Robin Watts <robin@peeves.(none)>
abc3ef
Date: Tue, 26 Mar 2013 13:09:49 -0700
abc3ef
Subject: [PATCH 2/5] Fix various memory squeezing errors.
abc3ef
abc3ef
Mostly these are unchecked allocations, but some are errors in the
abc3ef
cleanup paths.
abc3ef
---
abc3ef
 base/gslibctx.c | 14 +++++++++-----
abc3ef
 base/gsmalloc.c |  8 ++++++--
abc3ef
 psi/gs.c        |  3 ++-
abc3ef
 psi/imain.c     | 10 ++++++++--
abc3ef
 psi/interp.c    | 19 ++++++++++++++-----
abc3ef
 psi/zgstate.c   |  4 ++++
abc3ef
 6 files changed, 43 insertions(+), 15 deletions(-)
abc3ef
abc3ef
diff --git a/base/gslibctx.c b/base/gslibctx.c
abc3ef
index e0bf259..e3f1b06 100644
abc3ef
--- a/base/gslibctx.c
abc3ef
+++ b/base/gslibctx.c
abc3ef
@@ -192,13 +192,17 @@ int errwrite_nomem(const char *str, int len)
abc3ef
 int errwrite(const gs_memory_t *mem, const char *str, int len)
abc3ef
 {
abc3ef
     int code;
abc3ef
+    gs_lib_ctx_t *ctx;
abc3ef
     if (len == 0)
abc3ef
         return 0;
abc3ef
-    if (mem->gs_lib_ctx->stderr_fn)
abc3ef
-        return (*mem->gs_lib_ctx->stderr_fn)(mem->gs_lib_ctx->caller_handle, str, len);
abc3ef
-
abc3ef
-    code = fwrite(str, 1, len, mem->gs_lib_ctx->fstderr);
abc3ef
-    fflush(mem->gs_lib_ctx->fstderr);
abc3ef
+    ctx = mem->gs_lib_ctx;
abc3ef
+    if (ctx == NULL)
abc3ef
+      return 0;
abc3ef
+    if (ctx->stderr_fn)
abc3ef
+        return (*ctx->stderr_fn)(ctx->caller_handle, str, len);
abc3ef
+
abc3ef
+    code = fwrite(str, 1, len, ctx->fstderr);
abc3ef
+    fflush(ctx->fstderr);
abc3ef
     return code;
abc3ef
 }
abc3ef
 
abc3ef
diff --git a/base/gsmalloc.c b/base/gsmalloc.c
abc3ef
index 76f5d40..6b3d4b5 100644
abc3ef
--- a/base/gsmalloc.c
abc3ef
+++ b/base/gsmalloc.c
abc3ef
@@ -581,10 +581,14 @@ gs_malloc_init(void)
abc3ef
 void
abc3ef
 gs_malloc_release(gs_memory_t *mem)
abc3ef
 {
abc3ef
+    gs_malloc_memory_t * malloc_memory_default;
abc3ef
+
abc3ef
+    if (mem == NULL)
abc3ef
+        return;
abc3ef
 #ifdef USE_RETRY_MEMORY_WRAPPER
abc3ef
-    gs_malloc_memory_t * malloc_memory_default = gs_malloc_unwrap(mem);
abc3ef
+    malloc_memory_default = gs_malloc_unwrap(mem);
abc3ef
 #else
abc3ef
-    gs_malloc_memory_t * malloc_memory_default = (gs_malloc_memory_t *)mem;
abc3ef
+    malloc_memory_default = (gs_malloc_memory_t *)mem;
abc3ef
 #endif
abc3ef
     gs_lib_ctx_fin((gs_memory_t *)malloc_memory_default);
abc3ef
 
abc3ef
diff --git a/psi/gs.c b/psi/gs.c
abc3ef
index 530303b..dc5bb31 100644
abc3ef
--- a/psi/gs.c
abc3ef
+++ b/psi/gs.c
abc3ef
@@ -135,7 +135,8 @@ main(int argc, char *argv[])
abc3ef
             exit_status = 255;
abc3ef
     }
abc3ef
 
abc3ef
-    gs_to_exit_with_code(minst->heap, exit_status, code);
abc3ef
+    if (minst)
abc3ef
+        gs_to_exit_with_code(minst->heap, exit_status, code);
abc3ef
     gs_malloc_release(mem);
abc3ef
 
abc3ef
     switch (exit_status) {
abc3ef
diff --git a/psi/imain.c b/psi/imain.c
abc3ef
index aee66f8..91b0fbe 100644
abc3ef
--- a/psi/imain.c
abc3ef
+++ b/psi/imain.c
abc3ef
@@ -131,6 +131,7 @@ gs_main_init0(gs_main_instance * minst, FILE * in, FILE * out, FILE * err,
abc3ef
               int max_lib_paths)
abc3ef
 {
abc3ef
     ref *paths;
abc3ef
+    ref *array;
abc3ef
 
abc3ef
     /* Do platform-dependent initialization. */
abc3ef
     /* We have to do this as the very first thing, */
abc3ef
@@ -158,9 +159,14 @@ gs_main_init0(gs_main_instance * minst, FILE * in, FILE * out, FILE * err,
abc3ef
         gs_lib_finit(1, e_VMerror, minst->heap);
abc3ef
         return_error(e_VMerror);
abc3ef
     }
abc3ef
+    array = (ref *) gs_alloc_byte_array(minst->heap, max_lib_paths, sizeof(ref),
abc3ef
+                                        "lib_path array");
abc3ef
+    if (array == 0) {
abc3ef
+        gs_lib_finit(1, e_VMerror, minst->heap);
abc3ef
+        return_error(e_VMerror);
abc3ef
+    }
abc3ef
     make_array(&minst->lib_path.container, avm_foreign, max_lib_paths,
abc3ef
-               (ref *) gs_alloc_byte_array(minst->heap, max_lib_paths, sizeof(ref),
abc3ef
-                                           "lib_path array"));
abc3ef
+               array);
abc3ef
     make_array(&minst->lib_path.list, avm_foreign | a_readonly, 0,
abc3ef
                minst->lib_path.container.value.refs);
abc3ef
     minst->lib_path.env = 0;
abc3ef
diff --git a/psi/interp.c b/psi/interp.c
abc3ef
index bc8b49a..3e3aaaa 100644
abc3ef
--- a/psi/interp.c
abc3ef
+++ b/psi/interp.c
abc3ef
@@ -311,6 +311,7 @@ gs_interp_init(i_ctx_t **pi_ctx_p, const ref *psystem_dict,
abc3ef
 int
abc3ef
 gs_interp_alloc_stacks(gs_ref_memory_t *mem, gs_context_state_t * pcst)
abc3ef
 {
abc3ef
+    int code;
abc3ef
     gs_ref_memory_t *smem =
abc3ef
         (gs_ref_memory_t *)gs_memory_stable((gs_memory_t *)mem);
abc3ef
     ref stk;
abc3ef
@@ -318,16 +319,20 @@ gs_interp_alloc_stacks(gs_ref_memory_t *mem, gs_context_state_t * pcst)
abc3ef
 #define REFS_SIZE_OSTACK OS_REFS_SIZE(MAX_OSTACK)
abc3ef
 #define REFS_SIZE_ESTACK ES_REFS_SIZE(MAX_ESTACK)
abc3ef
 #define REFS_SIZE_DSTACK DS_REFS_SIZE(MAX_DSTACK)
abc3ef
-    gs_alloc_ref_array(smem, &stk, 0,
abc3ef
+    code = gs_alloc_ref_array(smem, &stk, 0,
abc3ef
                        REFS_SIZE_OSTACK + REFS_SIZE_ESTACK +
abc3ef
                        REFS_SIZE_DSTACK, "gs_interp_alloc_stacks");
abc3ef
+    if (code < 0)
abc3ef
+        return code;
abc3ef
 
abc3ef
     {
abc3ef
         ref_stack_t *pos = &pcst->op_stack.stack;
abc3ef
 
abc3ef
         r_set_size(&stk, REFS_SIZE_OSTACK);
abc3ef
-        ref_stack_init(pos, &stk, OS_GUARD_UNDER, OS_GUARD_OVER, NULL,
abc3ef
-                       smem, NULL);
abc3ef
+        code = ref_stack_init(pos, &stk, OS_GUARD_UNDER, OS_GUARD_OVER, NULL,
abc3ef
+                              smem, NULL);
abc3ef
+	if (code < 0)
abc3ef
+            return code;
abc3ef
         ref_stack_set_error_codes(pos, e_stackunderflow, e_stackoverflow);
abc3ef
         ref_stack_set_max_count(pos, MAX_OSTACK);
abc3ef
         stk.value.refs += REFS_SIZE_OSTACK;
abc3ef
@@ -339,8 +344,10 @@ gs_interp_alloc_stacks(gs_ref_memory_t *mem, gs_context_state_t * pcst)
abc3ef
 
abc3ef
         r_set_size(&stk, REFS_SIZE_ESTACK);
abc3ef
         make_oper(&euop, 0, estack_underflow);
abc3ef
-        ref_stack_init(pes, &stk, ES_GUARD_UNDER, ES_GUARD_OVER, &euop,
abc3ef
+        code = ref_stack_init(pes, &stk, ES_GUARD_UNDER, ES_GUARD_OVER, &euop,
abc3ef
                        smem, NULL);
abc3ef
+	if (code < 0)
abc3ef
+            return code;
abc3ef
         ref_stack_set_error_codes(pes, e_ExecStackUnderflow,
abc3ef
                                   e_execstackoverflow);
abc3ef
         /**************** E-STACK EXPANSION IS NYI. ****************/
abc3ef
@@ -353,7 +360,9 @@ gs_interp_alloc_stacks(gs_ref_memory_t *mem, gs_context_state_t * pcst)
abc3ef
         ref_stack_t *pds = &pcst->dict_stack.stack;
abc3ef
 
abc3ef
         r_set_size(&stk, REFS_SIZE_DSTACK);
abc3ef
-        ref_stack_init(pds, &stk, 0, 0, NULL, smem, NULL);
abc3ef
+        code = ref_stack_init(pds, &stk, 0, 0, NULL, smem, NULL);
abc3ef
+        if (code < 0)
abc3ef
+            return code;
abc3ef
         ref_stack_set_error_codes(pds, e_dictstackunderflow,
abc3ef
                                   e_dictstackoverflow);
abc3ef
         ref_stack_set_max_count(pds, MAX_DSTACK);
abc3ef
diff --git a/psi/zgstate.c b/psi/zgstate.c
abc3ef
index 8529d04..524f8ff 100644
abc3ef
--- a/psi/zgstate.c
abc3ef
+++ b/psi/zgstate.c
abc3ef
@@ -117,6 +117,8 @@ int_gstate_alloc(const gs_dual_memory_t * dmem)
abc3ef
 
abc3ef
     iigs = gs_alloc_struct((gs_memory_t *)lmem, int_gstate, &st_int_gstate,
abc3ef
                            "int_gstate_alloc(int_gstate)");
abc3ef
+    if (iigs == NULL)
abc3ef
+        return NULL;
abc3ef
     int_gstate_map_refs(iigs, make_null);
abc3ef
     make_empty_array(&iigs->dash_pattern_array, a_all);
abc3ef
     gs_alloc_ref_array(lmem, &proc0, a_readonly + a_executable, 2,
abc3ef
@@ -134,6 +136,8 @@ int_gstate_alloc(const gs_dual_memory_t * dmem)
abc3ef
     prci = gs_alloc_struct((gs_memory_t *)gmem, int_remap_color_info_t,
abc3ef
                            &st_int_remap_color_info,
abc3ef
                            "int_gstate_alloc(remap color info)");
abc3ef
+    if (prci == NULL)
abc3ef
+        return NULL;
abc3ef
     make_struct(&iigs->remap_color_info, imemory_space(gmem), prci);
abc3ef
     clear_pagedevice(iigs);
abc3ef
     gs_state_set_client(pgs, iigs, &istate_procs, true);
abc3ef
-- 
abc3ef
2.5.5
abc3ef
abc3ef
abc3ef
From 7bacac2668e96e14b7a988cfdc10c34405bfe585 Mon Sep 17 00:00:00 2001
abc3ef
From: Robin Watts <robin@peeves.(none)>
abc3ef
Date: Tue, 26 Mar 2013 14:48:04 -0700
abc3ef
Subject: [PATCH 3/5] More memory squeezing fixes.
abc3ef
abc3ef
Mostly unchecked allocation failures when setting up the gstate.
abc3ef
---
abc3ef
 base/gscolor.c  |  6 ++++--
abc3ef
 base/gsptype1.c |  4 +++-
abc3ef
 base/gsstate.c  | 35 +++++++++++++++++++++++++++++------
abc3ef
 base/gxchar.c   |  8 ++++++--
abc3ef
 base/gxdcolor.h |  2 +-
abc3ef
 5 files changed, 43 insertions(+), 12 deletions(-)
abc3ef
abc3ef
diff --git a/base/gscolor.c b/base/gscolor.c
abc3ef
index 62a687b..81f738d 100644
abc3ef
--- a/base/gscolor.c
abc3ef
+++ b/base/gscolor.c
abc3ef
@@ -210,7 +210,7 @@ gs_currenttransfer(const gs_state * pgs)
abc3ef
 /* ------ Non-operator routines ------ */
abc3ef
 
abc3ef
 /* Set device color = 1 for writing into the character cache. */
abc3ef
-void
abc3ef
+int
abc3ef
 gx_set_device_color_1(gs_state * pgs)
abc3ef
 {
abc3ef
     gs_color_space  *pcs;
abc3ef
@@ -222,7 +222,8 @@ gx_set_device_color_1(gs_state * pgs)
abc3ef
         gs_setcolorspace(pgs, pcs);
abc3ef
         rc_decrement_only_cs(pcs, "gx_set_device_color_1");
abc3ef
     } else {
abc3ef
-        /* {csrc} really need to signal an error here */
abc3ef
+        /* signal an error here */
abc3ef
+        return_error(gs_error_VMerror);
abc3ef
     }
abc3ef
     set_nonclient_dev_color(gs_currentdevicecolor_inline(pgs), 1);
abc3ef
     pgs->log_op = lop_default;
abc3ef
@@ -233,6 +234,7 @@ gx_set_device_color_1(gs_state * pgs)
abc3ef
     if (pgs->effective_overprint_mode == 1)
abc3ef
         (void)gs_do_set_overprint(pgs);
abc3ef
 
abc3ef
+    return 0;
abc3ef
 }
abc3ef
 
abc3ef
 /* ------ Internal routines ------ */
abc3ef
diff --git a/base/gsptype1.c b/base/gsptype1.c
abc3ef
index 171025d..0959bea 100644
abc3ef
--- a/base/gsptype1.c
abc3ef
+++ b/base/gsptype1.c
abc3ef
@@ -158,7 +158,9 @@ gs_pattern1_make_pattern(gs_client_color * pcc,
abc3ef
             gs_set_logical_op(saved, lop_default);
abc3ef
             break;
abc3ef
         case 2:         /* uncolored */
abc3ef
-            gx_set_device_color_1(saved);
abc3ef
+            code = gx_set_device_color_1(saved);
abc3ef
+            if (code < 0)
abc3ef
+                goto fsaved;
abc3ef
             break;
abc3ef
         default:
abc3ef
             code = gs_note_error(gs_error_rangecheck);
abc3ef
diff --git a/base/gsstate.c b/base/gsstate.c
abc3ef
index e3e5b70..a0419ff 100644
abc3ef
--- a/base/gsstate.c
abc3ef
+++ b/base/gsstate.c
abc3ef
@@ -223,8 +223,19 @@ gs_state_alloc(gs_memory_t * mem)
abc3ef
 
abc3ef
     if (pgs == 0)
abc3ef
         return 0;
abc3ef
-    pgs->saved = 0;
abc3ef
     *(gs_imager_state *)pgs = gstate_initial;   /* this sets is_gstate == true */
abc3ef
+    /* Need to set up at least enough to make gs_state_free happy */
abc3ef
+    pgs->saved = 0;
abc3ef
+    pgs->path = NULL;
abc3ef
+    pgs->clip_path = NULL;
abc3ef
+    pgs->clip_stack = NULL;
abc3ef
+    pgs->view_clip = NULL;
abc3ef
+    pgs->effective_clip_path = NULL;
abc3ef
+    pgs->font = NULL;
abc3ef
+    pgs->root_font = NULL;
abc3ef
+    pgs->show_gstate = NULL;
abc3ef
+    pgs->device = NULL;
abc3ef
+    pgs->dfilter_stack = NULL;
abc3ef
 
abc3ef
     /*
abc3ef
      * Just enough of the state is initialized at this point
abc3ef
@@ -247,6 +258,8 @@ gs_state_alloc(gs_memory_t * mem)
abc3ef
     pgs->clip_path = gx_cpath_alloc(mem, "gs_state_alloc(clip_path)");
abc3ef
     pgs->clip_stack = 0;
abc3ef
     pgs->view_clip = gx_cpath_alloc(mem, "gs_state_alloc(view_clip)");
abc3ef
+    if (pgs->view_clip == NULL)
abc3ef
+        goto fail;
abc3ef
     pgs->view_clip->rule = 0;   /* no clipping */
abc3ef
     pgs->effective_clip_id = pgs->clip_path->id;
abc3ef
     pgs->effective_view_clip_id = gs_no_id;
abc3ef
@@ -254,14 +267,24 @@ gs_state_alloc(gs_memory_t * mem)
abc3ef
     pgs->effective_clip_shared = true;
abc3ef
     /* Initialize things so that gx_remap_color won't crash. */
abc3ef
     pgs->color[0].color_space = gs_cspace_new_DeviceGray(pgs->memory);
abc3ef
+    if (pgs->color[0].color_space == NULL)
abc3ef
+        goto fail;
abc3ef
     pgs->color[1].color_space = gs_cspace_new_DeviceGray(pgs->memory);
abc3ef
+    if (pgs->color[1].color_space == NULL)
abc3ef
+        goto fail;
abc3ef
     pgs->in_cachedevice = 0;
abc3ef
     gs_swapcolors_quick(pgs); /* To color 1 */
abc3ef
-    gx_set_device_color_1(pgs); /* sets colorspace and client color */
abc3ef
+    code = gx_set_device_color_1(pgs); /* sets colorspace and client color */
abc3ef
+    if (code < 0)
abc3ef
+        goto fail;
abc3ef
     gs_swapcolors_quick(pgs); /* To color 0 */
abc3ef
-    gx_set_device_color_1(pgs); /* sets colorspace and client color */
abc3ef
+    code = gx_set_device_color_1(pgs); /* sets colorspace and client color */
abc3ef
+    if (code < 0)
abc3ef
+        goto fail;
abc3ef
     pgs->device = 0;            /* setting device adjusts refcts */
abc3ef
-    gs_nulldevice(pgs);
abc3ef
+    code = gs_nulldevice(pgs);
abc3ef
+    if (code < 0)
abc3ef
+        goto fail;
abc3ef
     gs_setalpha(pgs, 1.0);
abc3ef
     gs_settransfer(pgs, gs_identity_transfer);
abc3ef
     gs_setflat(pgs, 1.0);
abc3ef
@@ -886,7 +909,7 @@ gstate_free_parts(const gs_state * parts, gs_memory_t * mem, client_name_t cname
abc3ef
         gx_cpath_free(parts->effective_clip_path, cname);
abc3ef
     gx_cpath_free(parts->clip_path, cname);
abc3ef
     if (parts->path)
abc3ef
-	gx_path_free(parts->path, cname);
abc3ef
+        gx_path_free(parts->path, cname);
abc3ef
 }
abc3ef
 
abc3ef
 /* Allocate the privately allocated parts of a gstate. */
abc3ef
@@ -1223,4 +1246,4 @@ int gs_swapcolors(gs_state *pgs)
abc3ef
         return gs_do_set_overprint(pgs);
abc3ef
     }
abc3ef
     return 0;
abc3ef
-}
abc3ef
\ No newline at end of file
abc3ef
+}
abc3ef
diff --git a/base/gxchar.c b/base/gxchar.c
abc3ef
index 40c42f1..e2e8692 100644
abc3ef
--- a/base/gxchar.c
abc3ef
+++ b/base/gxchar.c
abc3ef
@@ -689,9 +689,13 @@ set_cache_device(gs_show_enum * penum, gs_state * pgs, floatp llx, floatp lly,
abc3ef
         clip_box.q.y = int2fixed(iheight);
abc3ef
         if ((code = gx_clip_to_rectangle(pgs, &clip_box)) < 0)
abc3ef
             return code;
abc3ef
-        gx_set_device_color_1(pgs);     /* write 1's */
abc3ef
+        code = gx_set_device_color_1(pgs);     /* write 1's */
abc3ef
+        if (code < 0)
abc3ef
+            return code;
abc3ef
         gs_swapcolors_quick(pgs);
abc3ef
-        gx_set_device_color_1(pgs);     /* write 1's */
abc3ef
+        code = gx_set_device_color_1(pgs);     /* write 1's */
abc3ef
+        if (code < 0)
abc3ef
+            return code;
abc3ef
         gs_swapcolors_quick(pgs);
abc3ef
         pgs->in_cachedevice = CACHE_DEVICE_CACHING;
abc3ef
     }
abc3ef
diff --git a/base/gxdcolor.h b/base/gxdcolor.h
abc3ef
index 476f59e..70b1291 100644
abc3ef
--- a/base/gxdcolor.h
abc3ef
+++ b/base/gxdcolor.h
abc3ef
@@ -300,7 +300,7 @@ extern  dev_color_proc_get_phase(gx_dc_ht_get_phase);
abc3ef
 
abc3ef
 /* Set up device color 1 for writing into a mask cache */
abc3ef
 /* (e.g., the character cache). */
abc3ef
-void gx_set_device_color_1(gs_state * pgs);
abc3ef
+int gx_set_device_color_1(gs_state * pgs);
abc3ef
 
abc3ef
 /* Remap the color if necessary. */
abc3ef
 int gx_remap_color(gs_state *);
abc3ef
-- 
abc3ef
2.5.5
abc3ef
abc3ef
abc3ef
From 0a2b1cddf39ddbe263c61b15af283aaf0f1d8d00 Mon Sep 17 00:00:00 2001
abc3ef
From: Robin Watts <robin.watts@artifex.com>
abc3ef
Date: Thu, 28 Mar 2013 19:42:34 +0000
abc3ef
Subject: [PATCH 4/5] Another memory squeezing fix.
abc3ef
abc3ef
Check allocations for failures. Free objects in the cleanup path.
abc3ef
---
abc3ef
 base/gsicc_manage.c | 8 +++++++-
abc3ef
 1 file changed, 7 insertions(+), 1 deletion(-)
abc3ef
abc3ef
diff --git a/base/gsicc_manage.c b/base/gsicc_manage.c
abc3ef
index 03cc29c..e0e7d93 100644
abc3ef
--- a/base/gsicc_manage.c
abc3ef
+++ b/base/gsicc_manage.c
abc3ef
@@ -1628,7 +1628,7 @@ gsicc_profile_new(stream *s, gs_memory_t *memory, const char* pname,
abc3ef
 {
abc3ef
     cmm_profile_t *result;
abc3ef
     int code;
abc3ef
-    char *nameptr;
abc3ef
+    char *nameptr = NULL;
abc3ef
     gs_memory_t *mem_nongc = memory->non_gc_memory;
abc3ef
 
abc3ef
     result = (cmm_profile_t*) gs_alloc_bytes(mem_nongc, sizeof(cmm_profile_t),
abc3ef
@@ -1639,6 +1639,10 @@ gsicc_profile_new(stream *s, gs_memory_t *memory, const char* pname,
abc3ef
     if (namelen > 0) {
abc3ef
         nameptr = (char*) gs_alloc_bytes(mem_nongc, namelen+1,
abc3ef
                              "gsicc_profile_new");
abc3ef
+        if (nameptr == NULL) {
abc3ef
+            gs_free_object(mem_nongc, result, "gsicc_profile_new");
abc3ef
+            return NULL;
abc3ef
+        }
abc3ef
         memcpy(nameptr, pname, namelen);
abc3ef
         nameptr[namelen] = '\0';
abc3ef
         result->name = nameptr;
abc3ef
@@ -1654,6 +1658,7 @@ gsicc_profile_new(stream *s, gs_memory_t *memory, const char* pname,
abc3ef
         code = gsicc_load_profile_buffer(result, s, mem_nongc);
abc3ef
         if (code < 0) {
abc3ef
             gs_free_object(mem_nongc, result, "gsicc_profile_new");
abc3ef
+            gs_free_object(mem_nongc, nameptr, "gsicc_profile_new");
abc3ef
             return NULL;
abc3ef
         }
abc3ef
     } else {
abc3ef
@@ -1670,6 +1675,7 @@ gsicc_profile_new(stream *s, gs_memory_t *memory, const char* pname,
abc3ef
     result->lock = gx_monitor_alloc(mem_nongc);
abc3ef
     if (result->lock == NULL ) {
abc3ef
         gs_free_object(mem_nongc, result, "gsicc_profile_new");
abc3ef
+        gs_free_object(mem_nongc, nameptr, "gsicc_profile_new");
abc3ef
         return(NULL);
abc3ef
     }
abc3ef
     if_debug1m(gs_debug_flag_icc, mem_nongc,
abc3ef
-- 
abc3ef
2.5.5
abc3ef
abc3ef
abc3ef
From e6997f1f7f436eb7ca2ebbc88a7463b880427b69 Mon Sep 17 00:00:00 2001
abc3ef
From: Michael Vrhel <michael.vrhel@artifex.com>
abc3ef
Date: Fri, 22 Apr 2016 09:24:52 -0700
abc3ef
Subject: [PATCH 5/5] Coverity ID 94568 Dereference after null check
abc3ef
abc3ef
The device profile is not even used in this function.
abc3ef
Likely left over by mistake.  Dereference of NULL no
abc3ef
longer possible here.
abc3ef
---
abc3ef
 base/gsicc.c | 6 ++----
abc3ef
 1 file changed, 2 insertions(+), 4 deletions(-)
abc3ef
abc3ef
diff --git a/base/gsicc.c b/base/gsicc.c
abc3ef
index e7093c5..f74f55b 100644
abc3ef
--- a/base/gsicc.c
abc3ef
+++ b/base/gsicc.c
abc3ef
@@ -606,10 +606,8 @@ gx_serialize_ICC(const gs_color_space * pcs, stream * s)
abc3ef
 static int
abc3ef
 gx_set_overprint_ICC(const gs_color_space * pcs, gs_state * pgs)
abc3ef
 {
abc3ef
-    gx_device *             dev = pgs->device;
abc3ef
-    gx_device_color_info *  pcinfo = (dev == 0 ? 0 : &dev->color_info);
abc3ef
-    cmm_dev_profile_t *dev_profile;
abc3ef
-    int code = dev_proc(dev, get_profile)(dev, &dev_profile);
abc3ef
+    gx_device *dev = pgs->device;
abc3ef
+    gx_device_color_info *pcinfo = (dev == 0 ? 0 : &dev->color_info);
abc3ef
 
abc3ef
     /* check if we require special handling */
abc3ef
     if ( !pgs->overprint                      ||
abc3ef
-- 
abc3ef
2.5.5
abc3ef