gh-123919: Fix null handling in `_freeze_module.c` (#123920)

This commit is contained in:
sobolevn 2024-09-11 16:37:35 +03:00 committed by GitHub
parent e9eedf19c9
commit c8d1dbef5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 0 deletions

View File

@ -110,6 +110,9 @@ static PyObject *
compile_and_marshal(const char *name, const char *text) compile_and_marshal(const char *name, const char *text)
{ {
char *filename = (char *) malloc(strlen(name) + 10); char *filename = (char *) malloc(strlen(name) + 10);
if (filename == NULL) {
return PyErr_NoMemory();
}
sprintf(filename, "<frozen %s>", name); sprintf(filename, "<frozen %s>", name);
PyObject *code = Py_CompileStringExFlags(text, filename, PyObject *code = Py_CompileStringExFlags(text, filename,
Py_file_input, NULL, 0); Py_file_input, NULL, 0);
@ -133,6 +136,9 @@ get_varname(const char *name, const char *prefix)
{ {
size_t n = strlen(prefix); size_t n = strlen(prefix);
char *varname = (char *) malloc(strlen(name) + n + 1); char *varname = (char *) malloc(strlen(name) + n + 1);
if (varname == NULL) {
return NULL;
}
(void)strcpy(varname, prefix); (void)strcpy(varname, prefix);
for (size_t i = 0; name[i] != '\0'; i++) { for (size_t i = 0; name[i] != '\0'; i++) {
if (name[i] == '.') { if (name[i] == '.') {
@ -178,6 +184,11 @@ write_frozen(const char *outpath, const char *inpath, const char *name,
fprintf(outfile, "%s\n", header); fprintf(outfile, "%s\n", header);
char *arrayname = get_varname(name, "_Py_M__"); char *arrayname = get_varname(name, "_Py_M__");
if (arrayname == NULL) {
fprintf(stderr, "memory error: could not allocate varname\n");
fclose(outfile);
return -1;
}
write_code(outfile, marshalled, arrayname); write_code(outfile, marshalled, arrayname);
free(arrayname); free(arrayname);