Skip to content

Commit

Permalink
src/__init__.py tests/: avoid segv from fz_samples_get() with empty p…
Browse files Browse the repository at this point in the history
…ixmap.

Pixmap.color_count(): don't raise exception if JM_color_count() returns empty
dict.

_read_samples(): return empty list if pixmap has no samples - avoids segv from
fz_samples_get().

Addresses #3848.
  • Loading branch information
julian-smith-artifex-com committed Oct 22, 2024
1 parent 5b49aa7 commit d94c817
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10171,8 +10171,6 @@ def color_count(self, colors=0, clip=None):
'''
pm = self.this
rc = JM_color_count( pm, clip)
if not rc:
raise RuntimeError( MSG_COLOR_COUNT_FAILED)
if not colors:
return len( rc)
return rc
Expand Down Expand Up @@ -14219,6 +14217,9 @@ def _read_samples( pixmap, offset, n):
# fixme: need to be able to get a sample in one call, as a Python
# bytes or similar.
ret = []
if not pixmap.samples():
# mupdf.fz_samples_get() gives a segv if pixmap->samples is null.
return ret
for i in range( n):
ret.append( mupdf.fz_samples_get( pixmap, offset + i))
return bytes( ret)
Expand Down
Binary file added tests/resources/test_3848.pdf
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test_pixmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,16 @@ def do(gi):
else:
assert out1 != out0
assert out2 == out0


def test_3848():
path = os.path.normpath(f'{__file__}/../../tests/resources/test_3848.pdf')
with pymupdf.open(path) as document:
for i in range(len(document)):
page = document.load_page(i)
print(f'{page=}.')
for annot in page.get_drawings():
if page.get_textbox(annot['rect']):
rect = annot['rect']
pixmap = page.get_pixmap(clip=rect)
color_bytes = pixmap.color_topusage()

0 comments on commit d94c817

Please sign in to comment.