From 9e553991e4c99814eb342d83b4fb42b5af457390 Mon Sep 17 00:00:00 2001
From: Shailesh Mistry <shailesh.mistry@hotmail.co.uk>
Date: Fri, 8 May 2020 21:50:30 +0100
Subject: [PATCH] Bug 697545 : Prevent numerous memory leaks.

Prevent memory leaks by propagating error codes and freeing loose objects.

Also resolve some compiler warnings.

Error created using :-
EMENTO_FAILAT=19484 ./membin/gpcl6 -sDEVICE=pbmraw -o /dev/null ./tests_private/pcl/pcl5efts/fts.0051
---
 base/gscspace.c | 11 +++++++----
 base/gxclrast.c | 10 ++++++++--
 base/gxcpath.c  |  4 +++-
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/base/gscspace.c b/base/gscspace.c
index aa3e3cf..f00bdba 100644
--- a/base/gscspace.c
+++ b/base/gscspace.c
@@ -329,8 +329,11 @@ gx_install_DeviceGray(gs_color_space * pcs, gs_gstate * pgs)
         return 0;
 
     /* If we haven't initialised the iccmanager, do it now. */
-    if (pgs->icc_manager->default_gray == NULL)
-        gsicc_init_iccmanager(pgs);
+    if (pgs->icc_manager->default_gray == NULL) {
+        int code = gsicc_init_iccmanager(pgs);
+        if (code < 0)
+            return code;
+    }
 
     /* pcs takes a reference to the default_gray profile data */
     pcs->cmm_icc_profile_data = pgs->icc_manager->default_gray;
@@ -677,7 +680,7 @@ int gx_set_overprint_cmyk(const gs_color_space * pcs, gs_gstate * pgs)
     }
 
     if_debug1m(gs_debug_flag_overprint, pgs->memory,
-        "[overprint] gx_set_overprint_cmyk. drawn_comps = 0x%x\n", drawn_comps);
+        "[overprint] gx_set_overprint_cmyk. drawn_comps = 0x%x\n", (uint)drawn_comps);
 
     if (drawn_comps == 0)
         return gx_spot_colors_set_overprint(pcs, pgs);
@@ -760,7 +763,7 @@ int gx_set_overprint_cmyk(const gs_color_space * pcs, gs_gstate * pgs)
 
     if_debug2m(gs_debug_flag_overprint, pgs->memory,
         "[overprint] gx_set_overprint_cmyk. retain_any_comps = %d, drawn_comps = 0x%x\n",
-        params.retain_any_comps, params.drawn_comps);
+        params.retain_any_comps, (uint)(params.drawn_comps));
 
     /* We are in CMYK, the profiles match and overprint is true.  Set effective
        overprint mode to overprint mode but only if effective has not already
diff --git a/base/gxclrast.c b/base/gxclrast.c
index 4e75d3e..65df9bd 100644
--- a/base/gxclrast.c
+++ b/base/gxclrast.c
@@ -612,6 +612,8 @@ in:                             /* Initialize for a new page. */
     memset(&gs_gstate, 0, sizeof(gs_gstate));
     GS_STATE_INIT_VALUES_CLIST((&gs_gstate));
     code = gs_gstate_initialize(&gs_gstate, mem);
+    if (code < 0)
+        goto out;
     gs_gstate.device = tdev;
     gs_gstate.view_clip = NULL; /* Avoid issues in pdf14 fill stroke */
     gs_gstate.clip_path = &clip_path;
@@ -620,7 +622,9 @@ in:                             /* Initialize for a new page. */
         code = gs_note_error(gs_error_VMerror);
         goto out;
     }
-    pcs->type->install_cspace(pcs, &gs_gstate);
+    code = pcs->type->install_cspace(pcs, &gs_gstate);
+    if (code < 0)
+        goto out;
     gs_gstate.color[0].color_space = pcs;
     rc_increment_cs(pcs);
     gs_gstate.color[1].color_space = pcs;
@@ -2383,6 +2387,8 @@ idata:                  data_size = 0;
     if (code < 0) {
         if (pfs.dev != NULL)
             term_patch_fill_state(&pfs);
+        gs_free_object(mem, pcs, "clist_playback_band(pcs)");
+        gs_free_object(mem, cbuf_storage, "clist_playback_band(cbuf_storage)");
         gx_cpath_free(&clip_path, "clist_playback_band");
         if (pcpath != &clip_path)
             gx_cpath_free(pcpath, "clist_playback_band");
diff --git a/base/gxcpath.c b/base/gxcpath.c
index d9518b8..082c389 100644
--- a/base/gxcpath.c
+++ b/base/gxcpath.c
@@ -403,8 +403,10 @@ gx_cpath_path_list_new(gs_memory_t *mem, gx_clip_path *pcpath, int rule,
     rc_init_free(pcplist, mem, 1, rc_free_cpath_path_list);
     if (pcpath!=NULL && !pcpath->path_valid) {
         code = gx_path_init_contained_shared(&pcplist->path, NULL, mem, cname);
-        if (code < 0)
+        if (code < 0) {
+            gs_free_object(mem, pcplist, "gx_cpath_path_list_new");
             return code;
+        }
         code = gx_cpath_to_path(pcpath, &pcplist->path);
     } else {
         gx_path_init_local(&pcplist->path, mem);
-- 
1.8.3.1

