From 72267b224a4412576b42f973a8b4060cc6542c92 Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Sat, 23 Nov 2019 16:14:24 +0100
Subject: [PATCH] add some error output to kstream

---
 pdf.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/pdf.c b/pdf.c
index 774c0b8..0406e51 100644
--- a/pdf.c
+++ b/pdf.c
@@ -345,9 +345,6 @@ kstream(HAllocator *mm__, const HParsedToken *x, void *env)
 	HBytes k;
 	size_t sz;
 
-	//fprintf(stderr, "kstream dict: %zu elems\n", dict->used);
-	//h_pprint(stderr, x, 0, 2);
-
 	/* look for the Length entry */
 	for (size_t i = 0; i < dict->used; i++) {
 		ent = dict->elements[i];
@@ -358,15 +355,20 @@ kstream(HAllocator *mm__, const HParsedToken *x, void *env)
 			break;
 		}
 	}
-	if (v == NULL || v->token_type != TT_UINT)
+	if (v == NULL || v->token_type != TT_SINT || v->sint < 0)
 		goto fail;
-	sz = (size_t)v->uint;
-		// XXX support indirect objects for the Length value?!
+	sz = (size_t)v->sint;
+		// XXX support indirect objects for the Length value!
 
 	return h_repeat_n__m(mm__, h_uint8__m(mm__), sz);
 fail:
-	//fprintf(stderr, "kstream fail.\n");
-	//if(v) fprintf(stderr, " v:%d\n", v->token_type);
+	if (v == NULL)
+		fprintf(stderr, "stream /Length missing\n");
+	else if (v -> token_type != TT_SINT)
+		fprintf(stderr, "stream /Length not an integer\n");
+	else if (v < 0)
+		fprintf(stderr, "stream /Length negative\n");
+	h_pprint(stderr, x, 0, 2);	// XXX debug
 	return h_nothing_p__m(mm__);
 }
 
-- 
GitLab