Skip to content

Commit cbfeb6a

Browse files
[3.12] gh-123919: Fix null handling in _freeze_module.c (GH-123920) (#123949)
gh-123919: Fix null handling in `_freeze_module.c` (GH-123920) (cherry picked from commit c8d1dbe) Co-authored-by: sobolevn <[email protected]>
1 parent 8ca75ee commit cbfeb6a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Programs/_freeze_module.c

+11
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ static PyObject *
123123
compile_and_marshal(const char *name, const char *text)
124124
{
125125
char *filename = (char *) malloc(strlen(name) + 10);
126+
if (filename == NULL) {
127+
return PyErr_NoMemory();
128+
}
126129
sprintf(filename, "<frozen %s>", name);
127130
PyObject *code = Py_CompileStringExFlags(text, filename,
128131
Py_file_input, NULL, 0);
@@ -146,6 +149,9 @@ get_varname(const char *name, const char *prefix)
146149
{
147150
size_t n = strlen(prefix);
148151
char *varname = (char *) malloc(strlen(name) + n + 1);
152+
if (varname == NULL) {
153+
return NULL;
154+
}
149155
(void)strcpy(varname, prefix);
150156
for (size_t i = 0; name[i] != '\0'; i++) {
151157
if (name[i] == '.') {
@@ -191,6 +197,11 @@ write_frozen(const char *outpath, const char *inpath, const char *name,
191197

192198
fprintf(outfile, "%s\n", header);
193199
char *arrayname = get_varname(name, "_Py_M__");
200+
if (arrayname == NULL) {
201+
fprintf(stderr, "memory error: could not allocate varname\n");
202+
fclose(outfile);
203+
return -1;
204+
}
194205
write_code(outfile, marshalled, arrayname);
195206
free(arrayname);
196207

0 commit comments

Comments
 (0)