Skip to content

Commit 3ad9234

Browse files
ROB: Handle images with empty data when processing an image from bytes (#2786)
Closes #2783.
1 parent d4df20d commit 3ad9234

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ history and [GitHub's 'Contributors' feature](https://github.com/py-pdf/pypdf/gr
1919
* [ediamondscience](https://github.com/ediamondscience)
2020
* [Ermeson, Felipe](https://github.com/FelipeErmeson)
2121
* [Freitag, François](https://github.com/francoisfreitag)
22+
* [Gagnon, William G.](https://github.com/williamgagnon)
2223
* [Górny, Michał](https://github.com/mgorny)
2324
* [Grillo, Miguel](https://github.com/Ineffable22)
2425
* [Gutteridge, David H.](https://github.com/dhgutteridge)

pypdf/_xobj_image_helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ._utils import check_if_whitespace_only, logger_warning
88
from .constants import ColorSpaces
9-
from .errors import PdfReadError
9+
from .errors import EmptyImageDataError, PdfReadError
1010
from .generic import (
1111
ArrayObject,
1212
DecodedStreamObject,
@@ -148,9 +148,12 @@ def _extended_image_frombytes(
148148
img = Image.frombytes(mode, size, data)
149149
except ValueError as exc:
150150
nb_pix = size[0] * size[1]
151-
if len(data) % nb_pix != 0:
151+
data_length = len(data)
152+
if data_length == 0:
153+
raise EmptyImageDataError("Data is 0 bytes, cannot process an image from empty data.") from exc
154+
if data_length % nb_pix != 0:
152155
raise exc
153-
k = nb_pix * len(mode) / len(data)
156+
k = nb_pix * len(mode) / data_length
154157
data = b"".join([bytes((x,) * int(k)) for x in data])
155158
img = Image.frombytes(mode, size, data)
156159
return img

pypdf/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@ class EmptyFileError(PdfReadError):
5959
"""Raised when a PDF file is empty or has no content."""
6060

6161

62+
class EmptyImageDataError(PyPdfError):
63+
"""Raised when trying to process an image that has no data."""
64+
65+
6266
STREAM_TRUNCATED_PREMATURELY = "Stream has ended unexpectedly"

tests/test_xobject_image_helpers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import pytest
55

66
from pypdf import PdfReader
7-
from pypdf._xobj_image_helpers import _handle_flate
8-
from pypdf.errors import PdfReadError
7+
from pypdf._xobj_image_helpers import _extended_image_frombytes, _handle_flate
8+
from pypdf.errors import EmptyImageDataError, PdfReadError
99
from pypdf.generic import ArrayObject, DecodedStreamObject, NameObject, NumberObject
1010

1111
from . import get_data_from_url
@@ -113,3 +113,12 @@ def test_handle_flate__image_mode_1():
113113
colors=2,
114114
obj_as_text="dummy",
115115
)
116+
117+
118+
def test_extended_image_frombytes_zero_data():
119+
mode = "RGB"
120+
size = (1, 1)
121+
data = b""
122+
123+
with pytest.raises(EmptyImageDataError, match="Data is 0 bytes, cannot process an image from empty data."):
124+
_extended_image_frombytes(mode, size, data)

0 commit comments

Comments
 (0)