From 88fc805ead84e3cff32b1309f9e9712a9f9fae9f Mon Sep 17 00:00:00 2001
From: Pompolic <pompolic@special-circumstanc.es>
Date: Thu, 10 Mar 2022 20:30:18 +0100
Subject: [PATCH] Add underflow check in act_rldstring

---
 pdf.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/pdf.c b/pdf.c
index b78944e..fb2f740 100644
--- a/pdf.c
+++ b/pdf.c
@@ -1156,15 +1156,22 @@ act_rldstring(const HParseResult *p, void *u)
 	size_t bytes_required;
 	uint8_t *result_bytes;
 
-	bytes_required = flattened_seq->used - 1;
-	result_bytes = h_arena_malloc(p->arena, sizeof(uint8_t) * bytes_required);
+	if (flattened_seq->used)
+	{
+		bytes_required = flattened_seq->used - 1;
+		result_bytes = h_arena_malloc(p->arena, sizeof(uint8_t) * bytes_required);
+
+		for (size_t i = 0; i < flattened_seq->used-1; ++i)
+		{
+			result_bytes[i] = H_CAST_UINT(flattened_seq->elements[i]);
+		}
 
-	for (size_t i = 0; i < flattened_seq->used-1; ++i)
+		return H_MAKE_BYTES(result_bytes, bytes_required);
+	}
+	else
 	{
-		result_bytes[i] = H_CAST_UINT(flattened_seq->elements[i]);
+		return NULL;
 	}
-
-	return H_MAKE_BYTES(result_bytes, bytes_required);
 }
 
 /*
-- 
GitLab