Skip to content

Instantly share code, notes, and snippets.

@robinhouston
Created December 9, 2011 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinhouston/1451817 to your computer and use it in GitHub Desktop.
Save robinhouston/1451817 to your computer and use it in GitHub Desktop.
A patch for pdftk 1.44 that fixes an infinite-loop bug
diff -ru -x '*.class' pdftk-1.44-dist/java/com/lowagie/text/pdf/PdfReader.java pdftk-1.44-patched/java/com/lowagie/text/pdf/PdfReader.java
--- pdftk-1.44-dist/java/com/lowagie/text/pdf/PdfReader.java 2010-10-28 21:07:44.000000000 +0000
+++ pdftk-1.44-patched/java/com/lowagie/text/pdf/PdfReader.java 2011-12-08 17:07:23.000000000 +0000
@@ -1421,18 +1421,18 @@
// ssteward - 6/21/10
// stream should be followed by a LF or a CRLF, but not just a CR, per the PDF spec.
// however, I have encountered a generated PDF (Microsoft Reporting Services 10.0.0.0)
- // that added a space after "stream" but before the CR; so gobble up unexpected chars
- // until we find a LF
+ // that added a space after "stream" but before the CR; and also one (HP Digital
+ // Sending Device) that follows the stream with just a CR, so handle these cases too.
int ch = tokens.read();
- /*
- if (ch != '\n')
+ if (ch == ' ')
ch = tokens.read();
- if (ch != '\n')
- tokens.backOnePosition(ch);
- */
- // ssteward
- while (ch != '\n')
- ch = tokens.read();
+ if (ch == '\r') {
+ ch = tokens.read();
+ if (ch != '\n')
+ tokens.backOnePosition(ch);
+ }
+ if (ch == -1)
+ tokens.throwError("Unexpected end of file");
PRStream stream = new PRStream(this, tokens.getFilePointer());
stream.putAll(dic);
diff -ru -x '*.class' pdftk-1.44-dist/java/com/lowagie/text/pdf/PRStream.java pdftk-1.44-patched/java/com/lowagie/text/pdf/PRStream.java
--- pdftk-1.44-dist/java/com/lowagie/text/pdf/PRStream.java 2010-10-28 21:07:45.000000000 +0000
+++ pdftk-1.44-patched/java/com/lowagie/text/pdf/PRStream.java 2011-12-08 15:06:20.000000000 +0000
@@ -262,6 +262,7 @@
crypto.prepareKey();
while (size > 0) {
int r = file.read(buf, 0, Math.min(size, buf.length));
+ if (r < 0) break;
size -= r;
if (decrypt != null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment