]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-123919: Fix null handling in `_freeze_module.c` (GH-123920) (#123949)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 11 Sep 2024 13:56:00 +0000 (15:56 +0200)
committerGitHub <noreply@github.com>
Wed, 11 Sep 2024 13:56:00 +0000 (13:56 +0000)
gh-123919: Fix null handling in `_freeze_module.c` (GH-123920)
(cherry picked from commit c8d1dbef5b770b647aa7ff45fd5b269bc7629d0b)

Co-authored-by: sobolevn <mail@sobolevn.me>
Programs/_freeze_module.c

index e55f1d56745c4dcab8f6bc10e7b95ddb657d2faf..c669d96c17351bd2f78cab98369ec0f836e47f43 100644 (file)
@@ -123,6 +123,9 @@ static PyObject *
 compile_and_marshal(const char *name, const char *text)
 {
     char *filename = (char *) malloc(strlen(name) + 10);
+    if (filename == NULL) {
+        return PyErr_NoMemory();
+    }
     sprintf(filename, "<frozen %s>", name);
     PyObject *code = Py_CompileStringExFlags(text, filename,
                                              Py_file_input, NULL, 0);
@@ -146,6 +149,9 @@ get_varname(const char *name, const char *prefix)
 {
     size_t n = strlen(prefix);
     char *varname = (char *) malloc(strlen(name) + n + 1);
+    if (varname == NULL) {
+        return NULL;
+    }
     (void)strcpy(varname, prefix);
     for (size_t i = 0; name[i] != '\0'; i++) {
         if (name[i] == '.') {
@@ -191,6 +197,11 @@ write_frozen(const char *outpath, const char *inpath, const char *name,
 
     fprintf(outfile, "%s\n", header);
     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);
     free(arrayname);