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

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