From 4e220a4c85c760bab0700ea1d80e4ce969675168 Mon Sep 17 00:00:00 2001
From: Pompolic <pompolic@special-circumstanc.es>
Date: Tue, 11 Jan 2022 22:14:21 +0100
Subject: [PATCH] Add validations to reject closing parentheses before a
 corresponding opening one

---
 pdf.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/pdf.c b/pdf.c
index 2cb5cd9..f8aa810 100644
--- a/pdf.c
+++ b/pdf.c
@@ -1176,6 +1176,9 @@ act_rldstring(const HParseResult *p, void *u)
  * e.g. parentheses in streams don't count, unless for some reason they're parsed with the lparen rule.)
  * The validations make the parse fail if this nesting depth is exceeded.
  * Because currently there are no diagnostic messages, this can probably result in unexpected parses.
+ *
+ * validate_rparen, validate_array_end, and validate_dclose will make the parse fail if a respective closing token ( ')', ']', '>>' )
+ * is encountered before an lparen, array_begin, or dopen.
  */
 
 #define PAREN_MAX_NEST_DEPTH 256
@@ -1196,6 +1199,13 @@ act_lparen(const HParseResult *p, void *u)
 	return H_MAKE_UINT(H_CAST_UINT(p->ast));
 }
 
+bool
+validate_rparen(HParseResult *p, void *u)
+{
+	struct Env *aux = (struct Env*) u;
+	return aux->paren_nest_depth > 0;
+}
+
 HParsedToken*
 act_rparen(const HParseResult *p, void *u)
 {
@@ -1224,6 +1234,13 @@ act_array_begin(const HParseResult *p, void *u)
 	return H_MAKE_UINT(H_CAST_UINT(p->ast));
 }
 
+bool
+validate_array_end(HParseResult *p, void *u)
+{
+	struct Env *aux = (struct Env*) u;
+	return aux->array_nest_depth > 0;
+}
+
 HParsedToken *
 act_array_end(const HParseResult *p, void *u)
 {
@@ -1252,6 +1269,13 @@ act_dopen(const HParseResult *p, void *u)
 	return H_MAKE_BYTES(H_CAST_BYTES(p->ast).token, H_CAST_BYTES(p->ast).len);
 }
 
+bool
+validate_dclose(HParseResult *p, void *u)
+{
+	struct Env *aux = (struct Env*) u;
+	return aux->dict_nest_depth > 0;
+}
+
 HParsedToken*
 act_dclose(const HParseResult *p, void *u)
 {
-- 
GitLab