Skip to content

_freeze_module.c has several unhandled nulls #123919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sobolevn opened this issue Sep 10, 2024 · 0 comments
Closed

_freeze_module.c has several unhandled nulls #123919

sobolevn opened this issue Sep 10, 2024 · 0 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Sep 10, 2024

Bug report

https://github.com/python/cpython/blob/main/Programs/_freeze_module.c has several unhandled nulls.

I have a PR ready:

diff --git Programs/_freeze_module.c Programs/_freeze_module.c
index 2a462a42cda..891e4256e89 100644
--- Programs/_freeze_module.c
+++ Programs/_freeze_module.c
@@ -110,6 +110,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);
@@ -133,6 +136,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] == '.') {
@@ -178,6 +184,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);

Linked PRs

@sobolevn sobolevn added the type-bug An unexpected behavior, bug, or error label Sep 10, 2024
@sobolevn sobolevn self-assigned this Sep 10, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Sep 10, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 11, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 11, 2024
kumaraditya303 pushed a commit that referenced this issue Sep 11, 2024
…#123949)

gh-123919: Fix null handling in `_freeze_module.c` (GH-123920)
(cherry picked from commit c8d1dbe)

Co-authored-by: sobolevn <[email protected]>
Yhg1s pushed a commit that referenced this issue Sep 30, 2024
…#123948)

gh-123919: Fix null handling in `_freeze_module.c` (GH-123920)
(cherry picked from commit c8d1dbe)

Co-authored-by: sobolevn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants