Skip to content

Commit

Permalink
Added test for #2548 and address rebase fail.
Browse files Browse the repository at this point in the history
Fixed test failure in rebased. We were not converting fz exception into C++
exception in src/extra.i:page_get_textpage(). Also fixed other cases where we
leaked fz exception.
  • Loading branch information
julian-smith-artifex-com committed Oct 6, 2023
1 parent a732f1f commit e9d58d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/extra.i
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ static pdf_obj *lll_JM_pdf_obj_from_str(fz_context *ctx, pdf_document *doc, cons
}

fz_catch(ctx) {
fz_rethrow(ctx);
mupdf::internal_throw_exception(ctx);
}

return result;
Expand Down Expand Up @@ -3744,7 +3744,7 @@ fz_stext_page* page_get_textpage(
fz_drop_device(ctx, dev);
}
fz_catch(ctx) {
return NULL;
mupdf::internal_throw_exception(ctx);
}
return tpage;
}
Expand Down Expand Up @@ -3965,7 +3965,7 @@ JM_new_buffer_from_stext_page(fz_stext_page *page)
}
fz_catch(ctx) {
fz_drop_buffer(ctx, buf);
fz_rethrow(ctx);
mupdf::internal_throw_exception(ctx);
}
return buf;
}
Expand Down Expand Up @@ -4209,7 +4209,7 @@ no_more_matches:;
fz_always(ctx)
fz_drop_buffer(ctx, buffer);
fz_catch(ctx)
fz_rethrow(ctx);
mupdf::internal_throw_exception(ctx);

return quads;
}
Expand Down
Binary file added tests/resources/test_2548.pdf
Binary file not shown.
27 changes: 27 additions & 0 deletions tests/test_2548.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

import fitz

root = os.path.abspath(f'{__file__}/../..')

def test_2548():
"""Text extraction should fail because of PDF structure cycle.
Old MuPDF version did not detect the loop.
"""
doc = fitz.open(f'{root}/tests/resources/test_2548.pdf')
e = False
for page in doc:
try:
_ = page.get_text()
except Exception as ee:
print(f'test_2548: {ee=}')
if hasattr(fitz, 'mupdf'):
# Rebased.
expected = "RuntimeError('code=2: cycle in structure tree')"
else:
# Classic.
expected = "RuntimeError('cycle in structure tree')"
assert repr(ee) == expected, f'Expected {expected=} but got {repr(ee)=}.'
e = True
assert e

0 comments on commit e9d58d7

Please sign in to comment.