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