From e0db637423ab6ea210d4c7e003f4884470f3ae69 Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Mon, 27 Feb 2023 15:07:33 +0000
Subject: [PATCH] validate the number of entries in xref subsections

Fixes #47.
---
 pdf.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/pdf.c b/pdf.c
index 5fea322..4077373 100644
--- a/pdf.c
+++ b/pdf.c
@@ -977,6 +977,21 @@ act_xrstment(const HParseResult *p, void *u)
 	return H_MAKE(XREntry, xr);
 }
 
+/*
+ * Validate that the declared number of entries in a cross-reference
+ * subsection matches the number that was actually parsed.
+ *
+ * p = ((nat nat) (xrent ...))
+ */
+bool
+validate_xrsub(HParseResult *p, void *u)
+{
+	uint64_t n = H_FIELD_UINT(0, 1);
+	HCountedArray *seq = H_FIELD_SEQ(1);
+
+	return (seq->used == n);
+}
+
 #define act_xrefs h_act_last
 
 /*
@@ -2477,7 +2492,7 @@ init_parser(struct Env *aux)
 	    xreol,
 	    VIOL(SEQ(h_many1(wchar)), "Nonconformant WS at end of xref entry (severity=1)")))));
 	H_RULE(xrhead,	SEQ(nat, IGN(sp), nat, nl));
-	H_RULE(xrsub,	SEQ(xrhead, h_many(xrent)));
+	H_VRULE(xrsub,	SEQ(xrhead, h_many(xrent)));
 	H_ARULE(xrefs,	SEQ(KW("xref"), nl, h_many(xrsub)));
 
 	/* cross-reference streams */
-- 
GitLab