micropython/shared/memzip/lexermemzip.c
Jim Mussared 5015779a6f py/builtinevex: Handle invalid filenames for execfile.
If a non-string buffer was passed to execfile, then it would be passed
as a non-null-terminated char* to mp_lexer_new_from_file.

This changes mp_lexer_new_from_file to take a qstr instead (as in almost
all cases a qstr will be created from this input anyway to set the
`__file__` attribute on the module).

This now makes execfile require a string (not generic buffer) argument,
which is probably a good fix to make anyway.

Fixes issue #12522.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-12 15:17:59 +11:00

20 lines
398 B
C

#include <stdlib.h>
#include "py/lexer.h"
#include "py/runtime.h"
#include "py/mperrno.h"
#include "memzip.h"
mp_lexer_t *mp_lexer_new_from_file(qstr filename)
{
void *data;
size_t len;
if (memzip_locate(qstr_str(filename), &data, &len) != MZ_OK) {
mp_raise_OSError(MP_ENOENT);
}
return mp_lexer_new_from_str_len(filename, (const char *)data, (mp_uint_t)len, 0);
}