From c2b82878375037869c185bfd146407b7b59ba218 Mon Sep 17 00:00:00 2001
From: Heinenen
Date: Mon, 12 Aug 2024 17:02:09 +0200
Subject: [PATCH] Detect reference cycles when parsing streams
---
src/nom_parser.rs | 27 +++++++++++++++++----------
src/reader.rs | 41 +++++++++++++++++++++++++++++------------
2 files changed, 46 insertions(+), 22 deletions(-)
diff --git a/src/nom_parser.rs b/src/nom_parser.rs
index 3d7b7a4..e8d2d3b 100644
--- a/src/nom_parser.rs
+++ b/src/nom_parser.rs
@@ -3,6 +3,7 @@ use crate::content::*;
use crate::error::XrefError;
use crate::xref::*;
use crate::Error;
+use std::collections::HashSet;
use std::str::{self, FromStr};
use nom::branch::alt;
@@ -270,12 +271,12 @@ fn dictionary(input: &[u8]) -> NomResult {
)(input)
}
-fn stream<'a>(input: &'a [u8], reader: &Reader) -> NomResult<'a, Object> {
+fn stream<'a>(input: &'a [u8], reader: &Reader, already_seen: &mut HashSet) -> NomResult<'a, Object> {
let (i, dict) = terminated(dictionary, tuple((space, tag(b"stream"), eol)))(input)?;
if let Ok(length) = dict.get(b"Length").and_then(|value| {
if let Ok(id) = value.as_reference() {
- reader.get_object(id).and_then(|value| value.as_i64())
+ reader.get_object(id, already_seen).and_then(|value| value.as_i64())
} else {
value.as_i64()
}
@@ -326,14 +327,17 @@ pub fn direct_object(input: &[u8]) -> Option