From 9aa1f4add6940e78abb18fb6c6a995ccd9800c0f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 May 2023 11:56:10 +0530 Subject: [PATCH] Fix loading of PDF from data causing memory corruption because PoDoFo now expects the data to outlive the document --- src/calibre/utils/podofo/doc.cpp | 3 +++ src/calibre/utils/podofo/global.h | 1 + 2 files changed, 4 insertions(+) diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index adf8899e02..67f49b110b 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -18,6 +18,7 @@ static void PDFDoc_dealloc(PDFDoc* self) { if (self->doc != NULL) delete self->doc; + Py_CLEAR(self->load_buffer_ref); Py_TYPE(self)->tp_free((PyObject*)self); } @@ -45,6 +46,8 @@ PDFDoc_load(PDFDoc *self, PyObject *args) { try { self->doc->LoadFromBuffer(bufferview(buffer, size)); + self->load_buffer_ref = args; + Py_INCREF(args); } catch(const PdfError & err) { podofo_set_exception(err); return NULL; diff --git a/src/calibre/utils/podofo/global.h b/src/calibre/utils/podofo/global.h index 3743f7640b..87a56cb04b 100644 --- a/src/calibre/utils/podofo/global.h +++ b/src/calibre/utils/podofo/global.h @@ -26,6 +26,7 @@ typedef struct { PyObject_HEAD /* Type-specific fields go here. */ PdfMemDocument *doc; + PyObject *load_buffer_ref; } PDFDoc;