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.
This commit is contained in:
ahgamut 2022-05-15 01:08:00 +05:30
parent ea2a5ffd6c
commit 0632853206

View file

@ -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;
}