From 06328532063fb391b1aa1fb9ae9c4dc29facac53 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Sun, 15 May 2022 01:08:00 +0530 Subject: [PATCH] fix issue pointed out by ubsan warning instead of reading the FILE* in little parts via PyMarshal_ReadObjectFromFile, now I just load the whole file into memory and call PyMarshal_ReadObjectFromString instead. This prevents ubsan warnings. --- third_party/python/Python/import.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/third_party/python/Python/import.c b/third_party/python/Python/import.c index 4a6ae2e61..c0a6a9d13 100644 --- a/third_party/python/Python/import.c +++ b/third_party/python/Python/import.c @@ -2436,6 +2436,8 @@ static PyObject *SFLObject_get_code(SourcelessFileLoader *self, PyObject *arg) { char *name = NULL; FILE *fp = NULL; PyObject *res = NULL; + char *rawbuf = NULL; + size_t rawlen = 0; if (!PyArg_Parse(arg, "z:get_code", &name)) return 0; if (!name) name = self->name; @@ -2472,8 +2474,16 @@ static PyObject *SFLObject_get_code(SourcelessFileLoader *self, PyObject *arg) { goto exit; } // return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path) - if (!(res = PyMarshal_ReadObjectFromFile(fp))) goto exit; + rawlen = stinfo.st_size - headerlen; + rawbuf = PyMem_RawMalloc(rawlen); + if (rawlen != fread(rawbuf, sizeof(char), rawlen, fp)) { + PyErr_Format(PyExc_ImportError, "reached EOF while size of source in %s\n", + name); + goto exit; + } + if (!(res = PyMarshal_ReadObjectFromString(rawbuf, rawlen))) goto exit; exit: + if (rawbuf) PyMem_RawFree(rawbuf); if (fp) fclose(fp); return res; }