Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB: Handle images with empty data when processing an image from bytes #2786

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading