IndexOutOfBoundsException in PdfReader
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 70/100
Línea de trabajo
Empieza por openpdf-core/src/main/java/org/openpdf/text/pdf/PdfReader.java y céntrate en readDocObj cerca de la línea indicada y en readXrefStream cerca de la línea 2294. Compara la comprobación de límites de getPdfObject(int idx) con la búsqueda en el flujo de objetos y, después, ejecuta o añade una prueba de regresión para un flujo xref malformado cuyo número de objeto supere la lista de objetos analizada. Se considera terminado cuando el análisis ya no lanza la IndexOutOfBoundsException sin procesar para esa entrada.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
IndexOutOfBoundsException in PdfReader
Describe the bug
java.lang.IndexOutOfBoundsException: Index 57407 out of bounds for length 899
at java.util.ArrayList.get
at com.lowagie.text.pdf.PdfReader.readDocObj(PdfReader.java:1957)
at com.lowagie.text.pdf.PdfReader.<init>
Root cause: The full stack shows the exception originates inside the OpenPDF library.
57407 is a PDF object number and 899 the parsed-object-list size — the classic signature of a broken PDF cross-reference table. Invoice.writePdf merges each invoice attachment's stored bytes, and PdfDocument.write re-parses each with OpenPDF's PdfReader. One attachment on this invoice is a corrupt/malformed PDF whose xref points at object 57407 while only 899 exist → the legacy reader throws a raw IndexOutOfBoundsException instead of a clean parse error.
Preview of a preliminary project invoice. Single occurrence for this exact stack signature in the retained window; deterministic for the one corrupt attachment, no loop / no data corruption / no retry-storm. Company & user details withheld here per sensitive-data policy.
Fails only the one request, but blocks every PDF render of that invoice (preview/print/likely send) until the bad attachment is removed. Same class recurs for any invoice carrying a corrupt PDF attachment.
Master branch:
https://github.com/LibrePDF/OpenPDF/blob/master/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfReader.java#L1965
2.0.3 version:
https://github.com/LibrePDF/OpenPDF/blob/2.0.3/openpdf/src/main/java/com/lowagie/text/pdf/PdfReader.java#L1957
Summary of the report: IndexOutOfBoundsException in PdfReader when parsing a PDF with a corrupt/malformed cross-reference (xref) stream — an object number referenced by the xref stream exceeds the size of the parsed object list.
What I verified in the current code (openpdf-core/src/main/java/org/openpdf/text/pdf/PdfReader.java):
- readXrefStream (~line 2294-2318): when parsing a cross-reference stream (type 2 entries, i.e. compressed objects living inside an object stream), it reads field2 — the object number of the containing object stream — directly from stream bytes and stores it as a key in objStmMark via objStmMark.put(on, seq). This value is never bounds-checked against the actual parsed object count; ensureXrefSize is only called for start/length (the compressed object's own number), not for field2.
- readDocObj (line 1965):
readObjStm((PRStream) xrefObj.get(n), h); - n comes straight from an objStmMark key — no range check before calling .get(n).
- Contrast with getPdfObject(int idx) (line 1738-1744), just ~200 lines above, which does the correct thing:
if (idx < 0 || idx >= xrefObj.size()) {
return null;
}
That inconsistency is exactly the bug: one accessor validates bounds, the other doesn't, and a malformed/corrupt xref stream can point at an object number (e.g. 57407) far beyond the actual object list size (e.g. 899), producing the raw ArrayList.get IndexOutOfBoundsException instead of a handled parse error.
Conclusion: Yes, still a valid, unfixed bug on master. It's a missing-bounds-check issue, not something masked by input sanitization elsewhere, and it also affects 2.0.3 as noted in the issue.
Fix would be small: add the same n < 0 || n >= xrefObj.size() guard at line 1965 (skip/log and continue rather than crash), and ideally validate field2 in readXrefStream before storing it in objStmMark.
System
- OS: Ubuntu Linux
- OpenPDF version: 2.0.3
Your real name
Andreas Røsdal
- Lenguaje dominante
- Java
- Estrellas
- 4.4k
- Forks
- 714
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de LibrePDF/OpenPDF
-
Bump brotli4j to 1.24.0Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
`PdfSmartCopy` can take effectively unbounded time on nested shared Form XObjectsPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abiertobug
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
-
enhancement
Dificultad 5/5 Más de una semana Aptitud para principiantes 38/100
-
Dificultad 4/5 1-2 días Aptitud para principiantes 45/100
Todos los issues de LibrePDF/OpenPDF
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
build
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
MetricsHub/metricshub-community#1472 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
IdentityArrayObjectSet iterator remove may throw ArrayIndexOutOfBoundsExceptionPosiblemente ocupada @jiteshkhatri11 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
javapathfinder/jpf-core#660 · 1 comentario ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día