Blob Blame Raw
diff --git a/poppler/Parser.cc b/poppler/Parser.cc
index bd4845ab..8f48efbe 100644
--- a/poppler/Parser.cc
+++ b/poppler/Parser.cc
@@ -203,6 +203,20 @@ Stream *Parser::makeStream(Object *dict,
   Goffset length;
   Goffset pos, endPos;
 
+  if (xref) {
+    XRefEntry *entry = xref->getEntry(objNum, gFalse);
+    if (entry) {
+      if (!entry->getFlag(XRefEntry::Parsing) ||
+          (objNum == 0 && objGen == 0)) {
+        entry->setFlag(XRefEntry::Parsing, gTrue);
+      } else {
+        error(errSyntaxError, getPos(),
+              "Object '{0:d} {1:d} obj' is being already parsed", objNum, objGen);
+        return NULL;
+      }
+    }
+  }
+
   // get stream start position
   lexer->skipToNextLine();
   if (!(str = lexer->getStream())) {
@@ -281,6 +295,16 @@ Stream *Parser::makeStream(Object *dict,
   // get filters
   str = str->addFilters(dict, recursion);
 
+  if (xref) {
+    // Don't try to reuse the entry from the block at the start
+    // of the function, xref can change in the middle because of
+    // reconstruction
+    XRefEntry *entry = xref->getEntry(objNum, gFalse);
+    if (entry) {
+      entry->setFlag(XRefEntry::Parsing, gFalse);
+    }
+  }
+
   return str;
 }
 
diff --git a/poppler/XRef.h b/poppler/XRef.h
index 11ee5e03..2eb2f9fd 100644
--- a/poppler/XRef.h
+++ b/poppler/XRef.h
@@ -72,7 +72,10 @@ struct XRefEntry {
 
     // Special flags -- available only after xref->scanSpecialFlags() is run
     Unencrypted, // Entry is stored in unencrypted form (meaningless in unencrypted documents)
-    DontRewrite  // Entry must not be written back in case of full rewrite
+    DontRewrite, // Entry must not be written back in case of full rewrite
+
+    // Regular flag (moved here to preserve values of previous flags)
+    Parsing      // Entry is currently being parsed
   };
 
   inline GBool getFlag(Flag flag) {