Skip to content

Commit

Permalink
Proof of concept formenv segfault
Browse files Browse the repository at this point in the history
If externalising the form fill environment to document level, the exit
call segfaults.
  • Loading branch information
mara004 committed Aug 30, 2022
1 parent 33f7966 commit 2b1ddbd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/pypdfium2/_helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
self._actual_input = input_data
self._rendering_input = None
self._ld_data = None
self._form_env = None

self._password = password
self._file_access = file_access
Expand Down Expand Up @@ -143,13 +144,31 @@ def close(self):
Close the document to release allocated memory.
This function shall be called when finished working with the object.
"""
if self._form_env is not None:
print(self._form_env)
pdfium.FPDFDOC_ExitFormFillEnvironment(self._form_env) # this call segfaults
pdfium.FPDF_CloseDocument(self.raw)
if self._ld_data is not None:
self._ld_data.close()
if self._autoclose and is_input_buffer(self._actual_input):
self._actual_input.close()


def init_formenv(self):
"""
Initialise a form environment object for this document.
If already initialised, the existing one will be returned instead.
:meth:`.close` will free the form environment, if initialised.
Returns:
FPDF_FORMHANDLE:
"""
if self._form_env is None:
form_info = pdfium.FPDF_FORMFILLINFO(1)
self._form_env = pdfium.FPDFDOC_InitFormFillEnvironment(self.raw, form_info)
return self._form_env


def get_version(self):
"""
Returns:
Expand Down
4 changes: 1 addition & 3 deletions src/pypdfium2/_helpers/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,8 @@ def render_base(
pdfium.FPDF_RenderPage_Close(self.raw)

if draw_forms:
form_info = pdfium.FPDF_FORMFILLINFO(1)
form_env = pdfium.FPDFDOC_InitFormFillEnvironment(self.pdf.raw, form_info)
form_env = self.pdf.init_formenv()
pdfium.FPDF_FFLDraw(form_env, *render_args)
pdfium.FPDFDOC_ExitFormFillEnvironment(form_env)

return buffer, cl_string, (width, height)

Expand Down

0 comments on commit 2b1ddbd

Please sign in to comment.