From e62f41f06c88bfdb4f46a418730095753c298b39 Mon Sep 17 00:00:00 2001
From: Pompolic <pompolic@special-circumstanc.es>
Date: Fri, 18 Feb 2022 16:15:58 +0100
Subject: [PATCH] Fix SIGSEGV in parse_xobject

Try to ignore text stream of a Form XObject if it fails to parse
---
 pdf.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/pdf.c b/pdf.c
index f2121fc..6bcd7e4 100644
--- a/pdf.c
+++ b/pdf.c
@@ -4444,16 +4444,25 @@ void parse_xobject(
 					//fprintf(stdout, "\nparse_xobject: Byte Stream = : ");
 					//h_pprintln(stdout, res->ast);
 
-					HBytes stm = H_CAST_BYTES(res->ast);
-					res = h_parse(p_textstream, stm.token, stm.len);
-
-					if (res) { // text found in stream
-						// DEBUG
-						//fprintf(stdout, "\nparse_xobject: Parsing text : ");
-						//h_pprintln(stdout, res->ast);
-
-//						xobj_r->node->xn.dict       = 0;
-						xobj_r->node->xn.textStream = res->ast;
+					/* Text stream is missing from object or failed to parse */
+					if (!res)
+					{
+						xobj_r->node->xn.textStream = NULL;
+						continue;
+					}
+					else
+					{
+						HBytes stm = H_CAST_BYTES(res->ast);
+						res = h_parse(p_textstream, stm.token, stm.len);
+
+						if (res) { // text found in stream
+							// DEBUG
+							//fprintf(stdout, "\nparse_xobject: Parsing text : ");
+							//h_pprintln(stdout, res->ast);
+
+//							xobj_r->node->xn.dict       = 0;
+							xobj_r->node->xn.textStream = res->ast;
+						}
 					}
 				}
 			}
-- 
GitLab