Blame SOURCES/ghostscript-cve-2018-19476.patch

483aca
From fac7eb144135f3ed8fbb0028ab1f33ce4dcc1985 Mon Sep 17 00:00:00 2001
483aca
From: Ken Sharp <ken.sharp@artifex.com>
483aca
Date: Fri, 21 Sep 2018 13:02:56 +0100
483aca
Subject: [PATCH 1/3] Check all uses of dict_find* to ensure 0 return properly
483aca
 handled
483aca
483aca
dict_find and friends have the surprising quirk of returning < 0 for
483aca
an error and > 0 for no error. But they can also return 0 which means
483aca
'not found' without it being an error.
483aca
483aca
From bug 699801, if the code assumes the usual case where 0 is a success
483aca
then an attempt might be made to use the empty dictionary slot returned
483aca
by dict_find*, which can lead to seg faults, and certainly won't have
483aca
the expected result.
483aca
---
483aca
 psi/icontext.c |  4 ++--
483aca
 psi/zcid.c     |  6 ++++--
483aca
 psi/zfapi.c    | 33 ++++++++++++++++++---------------
483aca
 psi/zfcid0.c   | 39 +++++++++++++++++++++++++++++----------
483aca
 psi/zfcid1.c   | 14 ++++++++++----
483aca
 psi/zicc.c     |  4 ++++
483aca
 psi/zpdf_r6.c  | 31 +++++++++++++++++++++++--------
483aca
 psi/ztoken.c   |  2 +-
483aca
 8 files changed, 91 insertions(+), 42 deletions(-)
483aca
483aca
diff --git a/psi/icontext.c b/psi/icontext.c
483aca
index 4db78e0..1fbe486 100644
483aca
--- a/psi/icontext.c
483aca
+++ b/psi/icontext.c
483aca
@@ -162,7 +162,7 @@ context_state_alloc(gs_context_state_t ** ppcst,
483aca
         uint size;
483aca
         ref *system_dict = &pcst->dict_stack.system_dict;
483aca
 
483aca
-        if (dict_find_string(system_dict, "userparams", &puserparams) >= 0)
483aca
+        if (dict_find_string(system_dict, "userparams", &puserparams) > 0)
483aca
             size = dict_length(puserparams);
483aca
         else
483aca
             size = 300;
483aca
@@ -286,7 +286,7 @@ context_state_store(gs_context_state_t * pcst)
483aca
         /* We need i_ctx_p for access to the d_stack. */
483aca
         i_ctx_t *i_ctx_p = pcst;
483aca
 
483aca
-        if (dict_find_string(systemdict, "userparams", &puserparams) < 0)
483aca
+        if (dict_find_string(systemdict, "userparams", &puserparams) <= 0)
483aca
             return_error(gs_error_Fatal);
483aca
         pcst->userparams = *puserparams;
483aca
     }
483aca
diff --git a/psi/zcid.c b/psi/zcid.c
483aca
index e394877..5c98fc9 100644
483aca
--- a/psi/zcid.c
483aca
+++ b/psi/zcid.c
483aca
@@ -72,11 +72,13 @@ TT_char_code_from_CID_no_subst(const gs_memory_t *mem,
483aca
     } else
483aca
         return false; /* Must not happen. */
483aca
     for (;n--; i++) {
483aca
+        int code;
483aca
+
483aca
         if (array_get(mem, DecodingArray, i, &char_code1) < 0 ||
483aca
             !r_has_type(&char_code1, t_integer))
483aca
             return false; /* Must not happen. */
483aca
-        if (dict_find(TT_cmap, &char_code1, &glyph_index) >= 0 &&
483aca
-                r_has_type(glyph_index, t_integer)) {
483aca
+        code = dict_find(TT_cmap, &char_code1, &glyph_index);
483aca
+        if (code > 0 && r_has_type(glyph_index, t_integer)) {
483aca
             *c = glyph_index->value.intval;
483aca
             found = true;
483aca
             if (*c != 0)
483aca
diff --git a/psi/zfapi.c b/psi/zfapi.c
483aca
index 48e1d54..1b687b0 100644
483aca
--- a/psi/zfapi.c
483aca
+++ b/psi/zfapi.c
483aca
@@ -1826,6 +1826,9 @@ FAPI_get_xlatmap(i_ctx_t *i_ctx_p, char **xlatmap)
483aca
 
483aca
     if ((code = dict_find_string(systemdict, ".xlatmap", &pref)) < 0)
483aca
         return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+
483aca
     if (r_type(pref) != t_string)
483aca
         return_error(gs_error_typecheck);
483aca
     *xlatmap = (char *)pref->value.bytes;
483aca
@@ -1881,11 +1884,11 @@ ps_get_server_param(gs_fapi_server *I, const byte *subtype,
483aca
     ref *FAPIconfig, *options, *server_options;
483aca
     i_ctx_t *i_ctx_p = (i_ctx_t *) I->client_ctx_p;
483aca
 
483aca
-    if (dict_find_string(systemdict, ".FAPIconfig", &FAPIconfig) >= 0
483aca
+    if (dict_find_string(systemdict, ".FAPIconfig", &FAPIconfig) > 0
483aca
         && r_has_type(FAPIconfig, t_dictionary)) {
483aca
-        if (dict_find_string(FAPIconfig, "ServerOptions", &options) >= 0
483aca
+        if (dict_find_string(FAPIconfig, "ServerOptions", &options) > 0
483aca
             && r_has_type(options, t_dictionary)) {
483aca
-            if (dict_find_string(options, (char *)subtype, &server_options) >=
483aca
+            if (dict_find_string(options, (char *)subtype, &server_options) >
483aca
                 0 && r_has_type(server_options, t_string)) {
483aca
                 *server_param = (byte *) server_options->value.const_bytes;
483aca
                 *server_param_size = r_size(server_options);
483aca
@@ -2070,7 +2073,7 @@ zFAPIrebuildfont(i_ctx_t *i_ctx_p)
483aca
     pdata = (font_data *) pfont->client_data;
483aca
     I = pbfont->FAPI;
483aca
 
483aca
-    if (dict_find_string((op - 1), "SubfontId", &v) >= 0
483aca
+    if (dict_find_string((op - 1), "SubfontId", &v) > 0
483aca
         && r_has_type(v, t_integer))
483aca
         subfont = v->value.intval;
483aca
     else
483aca
@@ -2277,8 +2280,8 @@ ps_get_glyphname_or_cid(gs_text_enum_t *penum,
483aca
         if (pbfont->FontType == ft_CID_TrueType && font_file_path) {
483aca
             ref *pdr2, *fidr, *dummy;
483aca
             pdr2 = pfont_dict(gs_rootfont(igs));
483aca
-            if (dict_find_string(pdr2, "FontInfo", &fidr) &&
483aca
-                dict_find_string(fidr, "GlyphNames2Unicode", &dummy))
483aca
+            if (dict_find_string(pdr2, "FontInfo", &fidr) > 0 &&
483aca
+                dict_find_string(fidr, "GlyphNames2Unicode", &dummy) > 0)
483aca
             {
483aca
                 unsigned char uc[4] = {0};
483aca
                 unsigned int cc = 0;
483aca
@@ -2417,13 +2420,13 @@ ps_get_glyphname_or_cid(gs_text_enum_t *penum,
483aca
 
483aca
                 fdict = pfont_dict(gs_rootfont(igs));
483aca
                 code = dict_find_string(fdict, "CMap", &CMapDict);
483aca
-                if (code >= 0 && r_has_type(CMapDict, t_dictionary)) {
483aca
+                if (code > 0 && r_has_type(CMapDict, t_dictionary)) {
483aca
                     code = dict_find_string(CMapDict, "WMode", &WMode);
483aca
-                    if (code >= 0 && r_has_type(WMode, t_integer)) {
483aca
+                    if (code > 0 && r_has_type(WMode, t_integer)) {
483aca
                         wmode = WMode->value.intval;
483aca
                     }
483aca
                     code = dict_find_string(CMapDict, "CMapName", &CMapName);
483aca
-                    if (code >= 0 && r_has_type(CMapName, t_name)) {
483aca
+                    if (code > 0 && r_has_type(CMapName, t_name)) {
483aca
                         name_string_ref(imemory, CMapName, &CMapNameStr);
483aca
                         cmapnm = (char *)CMapNameStr.value.bytes;
483aca
                         cmapnmlen = r_size(&CMapNameStr);
483aca
@@ -2432,10 +2435,10 @@ ps_get_glyphname_or_cid(gs_text_enum_t *penum,
483aca
                 /* We only have to lookup the char code if we're *not* using an identity ordering 
483aca
                    with the exception of Identity-UTF16 which is a different beast altogether */
483aca
                 if (unicode_cp || (cmapnmlen > 0 && !strncmp(cmapnm, utfcmap, cmapnmlen > utfcmaplen ? utfcmaplen : cmapnmlen))
483aca
-                    || (dict_find_string(pdr, "CIDSystemInfo", &CIDSystemInfo) >= 0
483aca
+                    || (dict_find_string(pdr, "CIDSystemInfo", &CIDSystemInfo) > 0
483aca
                     && r_has_type(CIDSystemInfo, t_dictionary)
483aca
                     && dict_find_string(CIDSystemInfo, "Ordering",
483aca
-                                        &Ordering) >= 0
483aca
+                                        &Ordering) > 0
483aca
                     && r_has_type(Ordering, t_string)
483aca
                     && strncmp((const char *)Ordering->value.bytes,
483aca
                                "Identity", 8) != 0)) {
483aca
@@ -2463,7 +2466,7 @@ ps_get_glyphname_or_cid(gs_text_enum_t *penum,
483aca
                 ref cc32;
483aca
                 ref *gid;
483aca
                 make_int(&cc32, 32);
483aca
-                if (dict_find(TT_cmap, &cc32, &gid) >= 0)
483aca
+                if (dict_find(TT_cmap, &cc32, &gid) > 0)
483aca
                     c = gid->value.intval;
483aca
             }
483aca
             cr->char_codes[0] = c;
483aca
@@ -2536,7 +2539,7 @@ ps_get_glyphname_or_cid(gs_text_enum_t *penum,
483aca
         if (dict_find_string(pdr, "CharStrings", &CharStrings) <= 0
483aca
             || !r_has_type(CharStrings, t_dictionary))
483aca
             return_error(gs_error_invalidfont);
483aca
-        if ((dict_find(CharStrings, &char_name, &glyph_index) < 0)
483aca
+        if ((dict_find(CharStrings, &char_name, &glyph_index) <= 0)
483aca
             || r_has_type(glyph_index, t_null)) {
483aca
 #ifdef DEBUG
483aca
             ref *pvalue;
483aca
@@ -2955,7 +2958,7 @@ zFAPIpassfont(i_ctx_t *i_ctx_p)
483aca
     if (code < 0)
483aca
         return code;
483aca
 
483aca
-    if (dict_find_string(op, "SubfontId", &v) >= 0
483aca
+    if (dict_find_string(op, "SubfontId", &v) > 0
483aca
         && r_has_type(v, t_integer))
483aca
         subfont = v->value.intval;
483aca
     else
483aca
@@ -2968,7 +2971,7 @@ zFAPIpassfont(i_ctx_t *i_ctx_p)
483aca
     /* If the font dictionary contains a FAPIPlugInReq key, the the PS world wants us
483aca
      * to try to use a specific FAPI plugin, so find it, and try it....
483aca
      */
483aca
-    if (dict_find_string(op, "FAPIPlugInReq", &v) >= 0 && r_type(v) == t_name) {
483aca
+    if (dict_find_string(op, "FAPIPlugInReq", &v) > 0 && r_type(v) == t_name) {
483aca
 
483aca
         name_string_ref(imemory, v, &reqstr);
483aca
 
483aca
diff --git a/psi/zfcid0.c b/psi/zfcid0.c
483aca
index 2aba09a..ba00b21 100644
483aca
--- a/psi/zfcid0.c
483aca
+++ b/psi/zfcid0.c
483aca
@@ -410,13 +410,25 @@ zbuildfont9(i_ctx_t *i_ctx_p)
483aca
      * from a file, GlyphData will be an integer, and DataSource will be
483aca
      * a (reusable) stream.
483aca
      */
483aca
-    if (code < 0 ||
483aca
-        (code = cid_font_data_param(op, &common, &GlyphDirectory)) < 0 ||
483aca
-        (code = dict_find_string(op, "FDArray", &prfda)) < 0 ||
483aca
-        (code = dict_find_string(op, "CIDFontName", &pCIDFontName)) <= 0 ||
483aca
-        (code = dict_int_param(op, "FDBytes", 0, MAX_FDBytes, -1, &FDBytes)) < 0
483aca
-        )
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    code = cid_font_data_param(op, &common, &GlyphDirectory);
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    code = dict_find_string(op, "FDArray", &prfda);
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+    code = dict_find_string(op, "CIDFontName", &pCIDFontName);
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+    code = dict_int_param(op, "FDBytes", 0, MAX_FDBytes, -1, &FDBytes);
483aca
+    if (code < 0)
483aca
         return code;
483aca
+
483aca
     /*
483aca
      * Since build_gs_simple_font may resize the dictionary and cause
483aca
      * pointers to become invalid, save CIDFontName
483aca
@@ -426,17 +438,24 @@ zbuildfont9(i_ctx_t *i_ctx_p)
483aca
         /* Standard CIDFont, require GlyphData and CIDMapOffset. */
483aca
         ref *pGlyphData;
483aca
 
483aca
-        if ((code = dict_find_string(op, "GlyphData", &pGlyphData)) < 0 ||
483aca
-            (code = dict_uint_param(op, "CIDMapOffset", 0, max_uint - 1,
483aca
-                                    max_uint, &CIDMapOffset)) < 0)
483aca
+        code = dict_find_string(op, "GlyphData", &pGlyphData);
483aca
+        if (code < 0)
483aca
+            return code;
483aca
+        if (code == 0)
483aca
+            return_error(gs_error_undefined);
483aca
+        code = dict_uint_param(op, "CIDMapOffset", 0, max_uint - 1, max_uint, &CIDMapOffset);
483aca
+        if (code < 0)
483aca
             return code;
483aca
         GlyphData = *pGlyphData;
483aca
         if (r_has_type(&GlyphData, t_integer)) {
483aca
             ref *pds;
483aca
             stream *ignore_s;
483aca
 
483aca
-            if ((code = dict_find_string(op, "DataSource", &pds)) < 0)
483aca
+            code = dict_find_string(op, "DataSource", &pds);
483aca
+            if (code < 0)
483aca
                 return code;
483aca
+            if (code == 0)
483aca
+                return_error(gs_error_undefined);
483aca
             check_read_file(i_ctx_p, ignore_s, pds);
483aca
             DataSource = *pds;
483aca
         } else {
483aca
diff --git a/psi/zfcid1.c b/psi/zfcid1.c
483aca
index ef3ece0..e3643a0 100644
483aca
--- a/psi/zfcid1.c
483aca
+++ b/psi/zfcid1.c
483aca
@@ -347,11 +347,17 @@ zbuildfont11(i_ctx_t *i_ctx_p)
483aca
     ref rcidmap, ignore_gdir, file, *pfile, cfnstr, *pCIDFontName, CIDFontName, *t;
483aca
     ulong loca_glyph_pos[2][2];
483aca
     int code = cid_font_data_param(op, &common, &ignore_gdir);
483aca
+    if (code < 0)
483aca
+        return code;
483aca
 
483aca
-    if (code < 0 ||
483aca
-        (code = dict_find_string(op, "CIDFontName", &pCIDFontName)) <= 0 ||
483aca
-        (code = dict_int_param(op, "MetricsCount", 0, 4, 0, &MetricsCount)) < 0
483aca
-        )
483aca
+    code = dict_find_string(op, "CIDFontName", &pCIDFontName);
483aca
+    if (code <= 0) {
483aca
+        if (code == 0)
483aca
+            return_error(gs_error_undefined);
483aca
+        return code;
483aca
+    }
483aca
+    code = dict_int_param(op, "MetricsCount", 0, 4, 0, &MetricsCount);
483aca
+    if (code < 0)
483aca
         return code;
483aca
     /*
483aca
      * Since build_gs_simple_font may resize the dictionary and cause
483aca
diff --git a/psi/zicc.c b/psi/zicc.c
483aca
index ebf25fe..53bdf34 100644
483aca
--- a/psi/zicc.c
483aca
+++ b/psi/zicc.c
483aca
@@ -261,6 +261,8 @@ zset_outputintent(i_ctx_t * i_ctx_p)
483aca
     code = dict_find_string(op, "N", &pnval);
483aca
     if (code < 0)
483aca
         return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
     ncomps = pnval->value.intval;
483aca
 
483aca
     /* verify the DataSource entry. Creat profile from stream */
483aca
@@ -491,6 +493,8 @@ znumicc_components(i_ctx_t * i_ctx_p)
483aca
     code = dict_find_string(op, "N", &pnval);
483aca
     if (code < 0)
483aca
         return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
     ncomps = pnval->value.intval;
483aca
     /* verify the DataSource entry. Create profile from stream */
483aca
     if (dict_find_string(op, "DataSource", &pstrmval) <= 0)
483aca
diff --git a/psi/zpdf_r6.c b/psi/zpdf_r6.c
483aca
index bcd4907..992f316 100644
483aca
--- a/psi/zpdf_r6.c
483aca
+++ b/psi/zpdf_r6.c
483aca
@@ -145,21 +145,36 @@ zcheck_r6_password(i_ctx_t * i_ctx_p)
483aca
         return_error(gs_error_typecheck);
483aca
     
483aca
     code = dict_find_string(CryptDict, "O", &Oref);
483aca
-    if (code < 0 || !r_has_type(Oref, t_string)) {
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+    if (!r_has_type(Oref, t_string))
483aca
       return_error(gs_error_typecheck);
483aca
-    }
483aca
+
483aca
     code = dict_find_string(CryptDict, "OE", &OEref);
483aca
-    if (code < 0 || !r_has_type(OEref, t_string)) {
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+    if (!r_has_type(OEref, t_string))
483aca
       return_error(gs_error_typecheck);
483aca
-    }
483aca
+
483aca
     code = dict_find_string(CryptDict, "U", &Uref);
483aca
-    if (code < 0 || !r_has_type(Uref, t_string)) {
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+    if (!r_has_type(Uref, t_string))
483aca
       return_error(gs_error_typecheck);
483aca
-    }
483aca
+
483aca
     code = dict_find_string(CryptDict, "UE", &UEref);
483aca
-    if (code < 0 || !r_has_type(UEref, t_string)) {
483aca
+    if (code < 0)
483aca
+        return code;
483aca
+    if (code == 0)
483aca
+        return_error(gs_error_undefined);
483aca
+    if (!r_has_type(UEref, t_string))
483aca
       return_error(gs_error_typecheck);
483aca
-    }
483aca
 
483aca
     pop(2);
483aca
     op = osp;
483aca
diff --git a/psi/ztoken.c b/psi/ztoken.c
483aca
index 519cd09..9314d97 100644
483aca
--- a/psi/ztoken.c
483aca
+++ b/psi/ztoken.c
483aca
@@ -356,7 +356,7 @@ ztoken_scanner_options(const ref *upref, int old_options)
483aca
         int code = dict_find_string(upref, pnso->pname, &ppcproc);
483aca
 
483aca
         /* Update the options only if the parameter has changed. */
483aca
-        if (code >= 0) {
483aca
+        if (code > 0) {
483aca
             if (r_has_type(ppcproc, t_null))
483aca
                 options &= ~pnso->option;
483aca
             else
483aca
-- 
483aca
2.17.2
483aca
483aca
483aca
From 434753adbe8be5534bfb9b7d91746023e8073d16 Mon Sep 17 00:00:00 2001
483aca
From: Ken Sharp <ken.sharp@artifex.com>
483aca
Date: Wed, 14 Nov 2018 09:25:13 +0000
483aca
Subject: [PATCH 2/3] Bug #700169 - unchecked type
483aca
483aca
Bug #700169 "Type confusion in setcolorspace"
483aca
483aca
In seticc() we extract "Name" from a dictionary, if it succeeds we then
483aca
use it as a string, without checking the type to see if it is in fact
483aca
a string.
483aca
483aca
Add a check on the type, and add a couple to check that 'N' is an integer
483aca
in a few places too.
483aca
---
483aca
 psi/zicc.c | 6 +++++-
483aca
 1 file changed, 5 insertions(+), 1 deletion(-)
483aca
483aca
diff --git a/psi/zicc.c b/psi/zicc.c
483aca
index 53bdf34..dbd2562 100644
483aca
--- a/psi/zicc.c
483aca
+++ b/psi/zicc.c
483aca
@@ -76,7 +76,7 @@ int seticc(i_ctx_t * i_ctx_p, int ncomps, ref *ICCdict, float *range_buff)
483aca
         want to have this buffer. */
483aca
     /* Check if we have the /Name entry.  This is used to associate with
483aca
        specs that have enumerated types to indicate sRGB sGray etc */
483aca
-    if (dict_find_string(ICCdict, "Name", &pnameval) > 0){
483aca
+    if (dict_find_string(ICCdict, "Name", &pnameval) > 0 && r_has_type(pnameval, t_string)){
483aca
         uint size = r_size(pnameval);
483aca
         char *str = (char *)gs_alloc_bytes(gs_gstate_memory(igs), size+1, "seticc");
483aca
         memcpy(str, (const char *)pnameval->value.bytes, size);
483aca
@@ -263,6 +263,8 @@ zset_outputintent(i_ctx_t * i_ctx_p)
483aca
         return code;
483aca
     if (code == 0)
483aca
         return_error(gs_error_undefined);
483aca
+    if (r_type(pnval) != t_integer)
483aca
+        return gs_note_error(gs_error_typecheck);
483aca
     ncomps = pnval->value.intval;
483aca
 
483aca
     /* verify the DataSource entry. Creat profile from stream */
483aca
@@ -495,6 +497,8 @@ znumicc_components(i_ctx_t * i_ctx_p)
483aca
         return code;
483aca
     if (code == 0)
483aca
         return_error(gs_error_undefined);
483aca
+    if (r_type(pnval) != t_integer)
483aca
+        return gs_note_error(gs_error_typecheck);
483aca
     ncomps = pnval->value.intval;
483aca
     /* verify the DataSource entry. Create profile from stream */
483aca
     if (dict_find_string(op, "DataSource", &pstrmval) <= 0)
483aca
-- 
483aca
2.17.2
483aca
483aca
483aca
From 9a1b3ac61761094713f44dedfce56013308a3b1d Mon Sep 17 00:00:00 2001
483aca
From: Ken Sharp <ken.sharp@artifex.com>
483aca
Date: Wed, 14 Nov 2018 09:31:10 +0000
483aca
Subject: [PATCH 3/3] PS interpreter - add some type checking
483aca
483aca
These were 'probably' safe anyway, since they mostly treat the objects
483aca
as integers without checking, which at least can't result in a crash.
483aca
483aca
Nevertheless, we ought to check.
483aca
483aca
The return from comparedictkeys could be wrong if one of the keys had
483aca
a value which was not an array, it could incorrectly decide the two
483aca
were in fact the same.
483aca
---
483aca
 psi/zbfont.c  | 15 +++++++++------
483aca
 psi/zcolor.c  | 24 +++++++++++++++++++++++-
483aca
 psi/zcrd.c    |  4 +++-
483aca
 psi/zfjpx.c   |  2 ++
483aca
 psi/zfont.c   |  3 +++
483aca
 psi/zfont0.c  |  3 +++
483aca
 psi/zimage3.c |  2 ++
483aca
 psi/ztrans.c  |  4 ++++
483aca
 8 files changed, 49 insertions(+), 8 deletions(-)
483aca
483aca
diff --git a/psi/zbfont.c b/psi/zbfont.c
483aca
index c1d0461..5b830a2 100644
483aca
--- a/psi/zbfont.c
483aca
+++ b/psi/zbfont.c
483aca
@@ -666,6 +666,9 @@ sub_font_params(gs_memory_t *mem, const ref *op, gs_matrix *pmat, gs_matrix *pom
483aca
         return_error(gs_error_invalidfont);
483aca
     if (dict_find_string(op, "OrigFont", &porigfont) <= 0)
483aca
         porigfont = NULL;
483aca
+    if (porigfont != NULL && !r_has_type(porigfont, t_dictionary))
483aca
+        return_error(gs_error_typecheck);
483aca
+
483aca
     if (pomat!= NULL) {
483aca
         if (porigfont == NULL ||
483aca
             dict_find_string(porigfont, "FontMatrix", &pmatrix) <= 0 ||
483aca
@@ -676,8 +679,8 @@ sub_font_params(gs_memory_t *mem, const ref *op, gs_matrix *pmat, gs_matrix *pom
483aca
     /* Use the FontInfo/OrigFontName key preferrentially (created by MS PSCRIPT driver) */
483aca
     if ((dict_find_string((porigfont != NULL ? porigfont : op), "FontInfo", &pfontinfo) > 0) &&
483aca
         r_has_type(pfontinfo, t_dictionary) &&
483aca
-        (dict_find_string(pfontinfo, "OrigFontName", &pfontname) > 0)) {
483aca
-        if ((dict_find_string(pfontinfo, "OrigFontStyle", &pfontstyle) > 0) &&
483aca
+        (dict_find_string(pfontinfo, "OrigFontName", &pfontname) > 0) && (r_has_type(pfontname, t_name) || r_has_type(pfontname, t_string))) {
483aca
+        if ((dict_find_string(pfontinfo, "OrigFontStyle", &pfontstyle) > 0) && (r_has_type(pfontname, t_name) || r_has_type(pfontname, t_string)) &&
483aca
                 r_size(pfontstyle) > 0) {
483aca
             const byte *tmpStr1 = pfontname->value.const_bytes;
483aca
             const byte *tmpStr2 = pfontstyle->value.const_bytes;
483aca
@@ -775,11 +778,11 @@ build_gs_font(i_ctx_t *i_ctx_p, os_ptr op, gs_font ** ppfont, font_type ftype,
483aca
             avm_space useglob = r_is_local(pencoding) ? avm_local : avm_global;
483aca
 
483aca
             ialloc_set_space(idmemory, useglob);
483aca
-            
483aca
+
483aca
             count = r_size(pencoding);
483aca
             if ((code = ialloc_ref_array(&penc, (r_type_attrs(pencoding) & a_readonly), count, "build_gs_font")) < 0)
483aca
                  return code;
483aca
-            
483aca
+
483aca
             while (count--) {
483aca
                ref r;
483aca
                if (array_get(imemory, pencoding, count, &r) < 0){
483aca
@@ -790,7 +793,7 @@ build_gs_font(i_ctx_t *i_ctx_p, os_ptr op, gs_font ** ppfont, font_type ftype,
483aca
                    ref_assign(&(penc.value.refs[count]), &r);
483aca
                }
483aca
                else {
483aca
-               
483aca
+
483aca
                    if ((code = obj_cvs(imemory, &r, (byte *)buf, 32, &size, (const byte **)(&bptr))) < 0) {
483aca
                        return(code);
483aca
                    }
483aca
@@ -799,7 +802,7 @@ build_gs_font(i_ctx_t *i_ctx_p, os_ptr op, gs_font ** ppfont, font_type ftype,
483aca
                    ref_assign(&(penc.value.refs[count]), &r);
483aca
                }
483aca
             }
483aca
-            
483aca
+
483aca
             if ((code = dict_put_string(osp, "Encoding", &penc, NULL)) < 0)
483aca
                return code;
483aca
             ialloc_set_space(idmemory, curglob);
483aca
diff --git a/psi/zcolor.c b/psi/zcolor.c
483aca
index fe81e79..b69b8f5 100644
483aca
--- a/psi/zcolor.c
483aca
+++ b/psi/zcolor.c
483aca
@@ -1877,7 +1877,12 @@ static int comparedictkey(i_ctx_t * i_ctx_p, ref *CIEdict1, ref *CIEdict2, char
483aca
     if (r_type(tempref1) == t_null)
483aca
         return 1;
483aca
 
483aca
-    return comparearrays(i_ctx_p, tempref1, tempref2);
483aca
+    code = comparearrays(i_ctx_p, tempref1, tempref2);
483aca
+
483aca
+    if (code > 0)
483aca
+        return 1;
483aca
+    else
483aca
+        return 0;
483aca
 }
483aca
 
483aca
 static int hasharray(i_ctx_t * i_ctx_p, ref *m1, gs_md5_state_t *md5)
483aca
@@ -5473,6 +5478,9 @@ static int seticcspace(i_ctx_t * i_ctx_p, ref *r, int *stage, int *cont, int CIE
483aca
                     return code;
483aca
                 if (code == 0)
483aca
                     return gs_note_error(gs_error_undefined);
483aca
+                if (r_type(tempref) != t_integer)
483aca
+                    return gs_note_error(gs_error_typecheck);
483aca
+
483aca
                 components = tempref->value.intval;
483aca
                 if (components > count_of(range)/2)
483aca
                     return_error(gs_error_rangecheck);
483aca
@@ -5584,6 +5592,10 @@ static int iccompareproc(i_ctx_t *i_ctx_p, ref *space, ref *testspace)
483aca
     /* Need to check all the various parts */
483aca
     code1 = dict_find_string(&ICCdict1, "N", &tempref1);
483aca
     code2 = dict_find_string(&ICCdict2, "N", &tempref2);
483aca
+
483aca
+    if (!r_has_type(tempref1, t_integer) || !r_has_type(tempref2, t_integer))
483aca
+        return 0;
483aca
+
483aca
     if (code1 != code2)
483aca
         return 0;
483aca
     if (tempref1->value.intval != tempref2->value.intval)
483aca
@@ -5737,6 +5749,8 @@ static int iccalternatespace(i_ctx_t * i_ctx_p, ref *space, ref **r, int *CIESub
483aca
         return code;
483aca
     if (code == 0)
483aca
         return gs_note_error(gs_error_undefined);
483aca
+    if (!r_has_type(tempref, t_integer))
483aca
+        return_error(gs_error_typecheck);
483aca
 
483aca
     components = tempref->value.intval;
483aca
 
483aca
@@ -5775,6 +5789,9 @@ static int icccomponents(i_ctx_t * i_ctx_p, ref *space, int *n)
483aca
         return code;
483aca
     if (code == 0)
483aca
         return gs_note_error(gs_error_undefined);
483aca
+    if (!r_has_type(tempref, t_integer))
483aca
+        return gs_note_error(gs_error_typecheck);
483aca
+
483aca
     *n = tempref->value.intval;
483aca
     return 0;
483aca
 }
483aca
@@ -5791,6 +5808,9 @@ static int iccdomain(i_ctx_t * i_ctx_p, ref *space, float *ptr)
483aca
         return code;
483aca
     if (code == 0)
483aca
         return gs_note_error(gs_error_undefined);
483aca
+    if (!r_has_type(tempref, t_integer))
483aca
+        return gs_note_error(gs_error_typecheck);
483aca
+
483aca
     components = tempref->value.intval;
483aca
     code = dict_find_string(&ICCdict, "Range", &tempref);
483aca
     if (code > 0 && !r_has_type(tempref, t_null)) {
483aca
@@ -5824,6 +5844,8 @@ static int iccrange(i_ctx_t * i_ctx_p, ref *space, float *ptr)
483aca
         return code;
483aca
     if (code == 0)
483aca
         return gs_note_error(gs_error_undefined);
483aca
+    if (!r_has_type(tempref, t_integer))
483aca
+        return gs_note_error(gs_error_typecheck);
483aca
     components = tempref->value.intval;
483aca
     code = dict_find_string(&ICCdict, "Range", &tempref);
483aca
     if (code > 0 && !r_has_type(tempref, t_null)) {
483aca
diff --git a/psi/zcrd.c b/psi/zcrd.c
483aca
index 7993b15..d58160d 100644
483aca
--- a/psi/zcrd.c
483aca
+++ b/psi/zcrd.c
483aca
@@ -231,8 +231,10 @@ zcrd1_params(os_ptr op, gs_cie_render * pcrd,
483aca
         return code;
483aca
 
483aca
     if (dict_find_string(op, "RenderTable", &pRT) > 0) {
483aca
-        const ref *prte = pRT->value.const_refs;
483aca
+        const ref *prte;
483aca
 
483aca
+        check_read_type(*pRT, t_array);
483aca
+        prte = pRT->value.const_refs;
483aca
         /* Finish unpacking and checking the RenderTable parameter. */
483aca
         check_type_only(prte[4], t_integer);
483aca
         if (!(prte[4].value.intval == 3 || prte[4].value.intval == 4))
483aca
diff --git a/psi/zfjpx.c b/psi/zfjpx.c
483aca
index c622f48..db1fae2 100644
483aca
--- a/psi/zfjpx.c
483aca
+++ b/psi/zfjpx.c
483aca
@@ -115,6 +115,8 @@ z_jpx_decode(i_ctx_t * i_ctx_p)
483aca
                                 dict_find_string(csdict, "N", &nref) > 0) {
483aca
                           if_debug1m('w', imemory, "[w] JPX image has an external %"PRIpsint
483aca
                                      " channel colorspace\n", nref->value.intval);
483aca
+                          if (r_type(nref) != t_integer)
483aca
+                              return gs_note_error(gs_error_typecheck);
483aca
                           switch (nref->value.intval) {
483aca
                             case 1: state.colorspace = gs_jpx_cs_gray;
483aca
                                 break;
483aca
diff --git a/psi/zfont.c b/psi/zfont.c
483aca
index 9c51792..f6c5ae1 100644
483aca
--- a/psi/zfont.c
483aca
+++ b/psi/zfont.c
483aca
@@ -596,6 +596,9 @@ zfont_info(gs_font *font, const gs_point *pscale, int members,
483aca
         info->members |= FONT_INFO_FULL_NAME;
483aca
     if ((members & FONT_INFO_EMBEDDING_RIGHTS)
483aca
         && (dict_find_string(pfontinfo, "FSType", &pvalue) > 0)) {
483aca
+        if (r_type(pvalue) != t_integer)
483aca
+            return gs_note_error(gs_error_typecheck);
483aca
+
483aca
         info->EmbeddingRights = pvalue->value.intval;
483aca
         info->members |= FONT_INFO_EMBEDDING_RIGHTS;
483aca
     }
483aca
diff --git a/psi/zfont0.c b/psi/zfont0.c
483aca
index 4b01c20..a179d7b 100644
483aca
--- a/psi/zfont0.c
483aca
+++ b/psi/zfont0.c
483aca
@@ -243,6 +243,9 @@ zbuildfont0(i_ctx_t *i_ctx_p)
483aca
         array_get(pfont->memory, &fdepvector, i, &fdep);
483aca
         /* The lookup can't fail, because of the pre-check above. */
483aca
         dict_find_string(&fdep, "FID", &pfid);
483aca
+        if (!r_has_type(pfid, t_fontID))
483aca
+            return gs_note_error(gs_error_typecheck);
483aca
+
483aca
         data.FDepVector[i] = r_ptr(pfid, gs_font);
483aca
     }
483aca
     pfont->data = data;
483aca
diff --git a/psi/zimage3.c b/psi/zimage3.c
483aca
index 87a3dce..2beda9f 100644
483aca
--- a/psi/zimage3.c
483aca
+++ b/psi/zimage3.c
483aca
@@ -53,6 +53,8 @@ zimage3(i_ctx_t *i_ctx_p)
483aca
         dict_find_string(op, "MaskDict", &pMaskDict) <= 0
483aca
         )
483aca
         return_error(gs_error_rangecheck);
483aca
+    check_type(*pDataDict, t_dictionary);
483aca
+    check_type(*pMaskDict, t_dictionary);
483aca
     if ((code = pixel_image_params(i_ctx_p, pDataDict,
483aca
                         (gs_pixel_image_t *)&image, &ip_data,
483aca
                         12, false, gs_currentcolorspace(igs))) < 0 ||
483aca
diff --git a/psi/ztrans.c b/psi/ztrans.c
483aca
index 64defda..0550a10 100644
483aca
--- a/psi/ztrans.c
483aca
+++ b/psi/ztrans.c
483aca
@@ -417,6 +417,7 @@ zimage3x(i_ctx_t *i_ctx_p)
483aca
     gs_image3x_t_init(&image, NULL);
483aca
     if (dict_find_string(op, "DataDict", &pDataDict) <= 0)
483aca
         return_error(gs_error_rangecheck);
483aca
+    check_type(*pDataDict, t_dictionary);
483aca
     if ((code = pixel_image_params(i_ctx_p, pDataDict,
483aca
                    (gs_pixel_image_t *)&image, &ip_data,
483aca
                    16, false, gs_currentcolorspace(igs))) < 0 ||
483aca
@@ -453,6 +454,9 @@ image_params *pip_data, const char *dict_name,
483aca
 
483aca
     if (dict_find_string(op, dict_name, &pMaskDict) <= 0)
483aca
         return 1;
483aca
+    if (!r_has_type(pMaskDict, t_dictionary))
483aca
+        return gs_note_error(gs_error_typecheck);
483aca
+
483aca
     if ((mcode = code = data_image_params(mem, pMaskDict, &pixm->MaskDict,
483aca
                                           &ip_mask, false, 1, 16, false, false)) < 0 ||
483aca
         (code = dict_int_param(pMaskDict, "ImageType", 1, 1, 0, &ignored)) < 0 ||
483aca
-- 
483aca
2.17.2
483aca