Skip to content

Commit

Permalink
ROB: Handle images with empty data when processing an image from bytes (
Browse files Browse the repository at this point in the history
#2786)

Closes #2783.
  • Loading branch information
williamgagnonpoka authored Aug 2, 2024
1 parent d4df20d commit 3ad9234
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ history and [GitHub's 'Contributors' feature](https://github.com/py-pdf/pypdf/gr
* [ediamondscience](https://github.com/ediamondscience)
* [Ermeson, Felipe](https://github.com/FelipeErmeson)
* [Freitag, François](https://github.com/francoisfreitag)
* [Gagnon, William G.](https://github.com/williamgagnon)
* [Górny, Michał](https://github.com/mgorny)
* [Grillo, Miguel](https://github.com/Ineffable22)
* [Gutteridge, David H.](https://github.com/dhgutteridge)
Expand Down
9 changes: 6 additions & 3 deletions pypdf/_xobj_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ._utils import check_if_whitespace_only, logger_warning
from .constants import ColorSpaces
from .errors import PdfReadError
from .errors import EmptyImageDataError, PdfReadError
from .generic import (
ArrayObject,
DecodedStreamObject,
Expand Down Expand Up @@ -148,9 +148,12 @@ def _extended_image_frombytes(
img = Image.frombytes(mode, size, data)
except ValueError as exc:
nb_pix = size[0] * size[1]
if len(data) % nb_pix != 0:
data_length = len(data)
if data_length == 0:
raise EmptyImageDataError("Data is 0 bytes, cannot process an image from empty data.") from exc
if data_length % nb_pix != 0:
raise exc
k = nb_pix * len(mode) / len(data)
k = nb_pix * len(mode) / data_length
data = b"".join([bytes((x,) * int(k)) for x in data])
img = Image.frombytes(mode, size, data)
return img
Expand Down
4 changes: 4 additions & 0 deletions pypdf/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ class EmptyFileError(PdfReadError):
"""Raised when a PDF file is empty or has no content."""


class EmptyImageDataError(PyPdfError):
"""Raised when trying to process an image that has no data."""


STREAM_TRUNCATED_PREMATURELY = "Stream has ended unexpectedly"
13 changes: 11 additions & 2 deletions tests/test_xobject_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pytest

from pypdf import PdfReader
from pypdf._xobj_image_helpers import _handle_flate
from pypdf.errors import PdfReadError
from pypdf._xobj_image_helpers import _extended_image_frombytes, _handle_flate
from pypdf.errors import EmptyImageDataError, PdfReadError
from pypdf.generic import ArrayObject, DecodedStreamObject, NameObject, NumberObject

from . import get_data_from_url
Expand Down Expand Up @@ -113,3 +113,12 @@ def test_handle_flate__image_mode_1():
colors=2,
obj_as_text="dummy",
)


def test_extended_image_frombytes_zero_data():
mode = "RGB"
size = (1, 1)
data = b""

with pytest.raises(EmptyImageDataError, match="Data is 0 bytes, cannot process an image from empty data."):
_extended_image_frombytes(mode, size, data)

0 comments on commit 3ad9234

Please sign in to comment.