|  | 
| 20 | 20 |     Transformation, | 
| 21 | 21 | ) | 
| 22 | 22 | from pypdf.annotations import Link | 
| 23 |  | -from pypdf.errors import DeprecationError, PageSizeNotDefinedError, PyPdfError | 
|  | 23 | +from pypdf.errors import DeprecationError, PageSizeNotDefinedError, PdfReadError, PyPdfError | 
| 24 | 24 | from pypdf.generic import ( | 
| 25 | 25 |     ArrayObject, | 
| 26 | 26 |     ByteStringObject, | 
| @@ -2851,3 +2851,26 @@ def test_unterminated_object__with_incremental_writer(): | 
| 2851 | 2851 |     writer.write(fi) | 
| 2852 | 2852 |     b = fi.getvalue() | 
| 2853 | 2853 |     assert b[-39:] == b"\nendstream\nendobj\nstartxref\n1240\n%%EOF\n" | 
|  | 2854 | + | 
|  | 2855 | + | 
|  | 2856 | +def test_wrong_size_in_incremental_pdf(caplog): | 
|  | 2857 | +    source_data = RESOURCE_ROOT.joinpath("crazyones.pdf").read_bytes() | 
|  | 2858 | +    writer = PdfWriter(BytesIO(source_data), incremental=True) | 
|  | 2859 | +    writer._add_object(DictionaryObject()) | 
|  | 2860 | + | 
|  | 2861 | +    incremental_data = BytesIO() | 
|  | 2862 | +    writer.write(incremental_data) | 
|  | 2863 | +    modified_data = incremental_data.getvalue().replace(b"/Size 25", b"/Size 2") | 
|  | 2864 | + | 
|  | 2865 | +    writer = PdfWriter(BytesIO(modified_data), incremental=False) | 
|  | 2866 | +    assert "Object count 19 exceeds defined trailer size 2" in caplog.text | 
|  | 2867 | +    assert len(writer._objects) == 20 | 
|  | 2868 | + | 
|  | 2869 | +    caplog.clear() | 
|  | 2870 | +    writer = PdfWriter(incremental=False) | 
|  | 2871 | +    writer.strict = True | 
|  | 2872 | +    with pytest.raises(expected_exception=PdfReadError, match=r"^Object count 19 exceeds defined trailer size 2$"): | 
|  | 2873 | +        writer.clone_reader_document_root(reader=PdfReader(BytesIO(modified_data))) | 
|  | 2874 | + | 
|  | 2875 | +    with pytest.raises(expected_exception=PdfReadError, match=r"^Got index error while flattening\.$"): | 
|  | 2876 | +        PdfWriter(BytesIO(modified_data), incremental=True) | 
0 commit comments