From b2bacc71f7a83d51df91a206a3e23a81a5f18da4 Mon Sep 17 00:00:00 2001
From: Pompolic <pompolic@special-circumstanc.es>
Date: Mon, 14 Mar 2022 18:33:19 +0100
Subject: [PATCH] Cherry-pick f2e844

Original message:
commit f2e844e917c30e02cce387e3ebae68f97e96a8af
Author: sumit.ray@baesystems.com <sumit.ray@baesystems.com>
Date:   Fri Mar 11 12:56:13 2022 -0500

    Added a guard and cause the program to exit gracefully if the required Size field is missing or malformed.
---
 pdf.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/pdf.c b/pdf.c
index 01ebf57..5c183b2 100644
--- a/pdf.c
+++ b/pdf.c
@@ -5630,8 +5630,15 @@ parse_xrefs(struct Env *aux)
 		/* process the root */
 		if (processRoot) {
 			// Size is a required field in the trailer dictionary
-			trailer = H_INDEX(Dict, res->ast, 1);
-			maxObjNum = H_CAST_SINT(dictentry(trailer, "Size"));
+			const HParsedToken *trailer_t = H_INDEX_TOKEN(res->ast, 1);
+			trailer = H_CAST(Dict, trailer_t);
+			const HParsedToken *size_t =  dictentry(trailer, "Size");
+			if (size_t == NULL || size_t->token_type != TT_SINT) {
+				fprintf(stderr, "VIOLATION[5]: error parsing trailer section!"
+							    "Missing or malformed -Size- field\n");
+				return;
+			}
+			maxObjNum = H_CAST_SINT(size_t);
 
 			processRoot = false;
 		}
-- 
GitLab