From c991e530d009a5a9a3d53601765bd1bb5f7128db Mon Sep 17 00:00:00 2001 From: deeplow Date: Mon, 5 Feb 2024 14:27:43 +0000 Subject: [PATCH] Fix IsolationProvider.percentage variable reuse If one converted more than one document, since the state of IsolationProvider.percentage would be stored in the IsolationProvider instance, it would get reused for the second document. The fix is to keep it as a local variable, but we can explore having progress stored on the document itself, for example. Or having one IsolationProvider per conversion. --- dangerzone/isolation_provider/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dangerzone/isolation_provider/base.py b/dangerzone/isolation_provider/base.py index cf21eaa..c7e46ed 100644 --- a/dangerzone/isolation_provider/base.py +++ b/dangerzone/isolation_provider/base.py @@ -52,7 +52,6 @@ class IsolationProvider(ABC): STARTUP_TIME_SECONDS = 0 # The maximum time it takes a the provider to start up. def __init__(self) -> None: - self.percentage = 0.0 self.proc: Optional[subprocess.Popen] = None if getattr(sys, "dangerzone_dev", False) == True: @@ -98,6 +97,7 @@ class IsolationProvider(ABC): document.mark_as_failed() def doc_to_pixels(self, document: Document, tempdir: str) -> None: + percentage = 0.0 with open(document.input_filename, "rb") as f: self.proc = self.start_doc_to_pixels_proc() try: @@ -125,7 +125,7 @@ class IsolationProvider(ABC): sw.start() for page in range(1, n_pages + 1): text = f"Converting page {page}/{n_pages} to pixels" - self.print_progress_trusted(document, False, text, self.percentage) + self.print_progress_trusted(document, False, text, percentage) width = read_int(self.proc.stdout, timeout=sw.remaining) height = read_int(self.proc.stdout, timeout=sw.remaining) @@ -149,14 +149,14 @@ class IsolationProvider(ABC): with open(f"{tempdir}/pixels/page-{page}.rgb", "wb") as f_rgb: f_rgb.write(untrusted_pixels) - self.percentage += percentage_per_page + percentage += percentage_per_page # Ensure nothing else is read after all bitmaps are obtained self.proc.stdout.close() # TODO handle leftover code input text = "Converted document to pixels" - self.print_progress_trusted(document, False, text, self.percentage) + self.print_progress_trusted(document, False, text, percentage) if getattr(sys, "dangerzone_dev", False): assert self.proc.stderr is not None