Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5378e2

Browse files
committed
Now all error paths of _freeze_importlib use 'goto error' and the error label cleans up all used resources.
2 parents c3e10c2 + 43d82df commit e5378e2

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

Modules/_freeze_importlib.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ int
3434
main(int argc, char *argv[])
3535
{
3636
char *inpath, *outpath;
37-
FILE *infile, *outfile = NULL;
37+
FILE *infile = NULL, *outfile = NULL;
3838
struct stat st;
3939
size_t text_size, data_size, n;
40-
char *text;
40+
char *text = NULL;
4141
unsigned char *data;
42-
PyObject *code, *marshalled;
42+
PyObject *code = NULL, *marshalled = NULL;
4343

4444
PyImport_FrozenModules = _PyImport_FrozenModules;
4545

@@ -52,28 +52,25 @@ main(int argc, char *argv[])
5252
infile = fopen(inpath, "rb");
5353
if (infile == NULL) {
5454
fprintf(stderr, "cannot open '%s' for reading\n", inpath);
55-
return 1;
55+
goto error;
5656
}
5757
if (fstat(fileno(infile), &st)) {
58-
fclose(infile);
5958
fprintf(stderr, "cannot fstat '%s'\n", inpath);
60-
return 1;
59+
goto error;
6160
}
6261
text_size = st.st_size;
6362
text = (char *) malloc(text_size + 1);
6463
if (text == NULL) {
65-
fclose(infile);
6664
fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
67-
return 1;
65+
goto error;
6866
}
6967
n = fread(text, 1, text_size, infile);
7068
fclose(infile);
7169
infile = NULL;
7270
if (n < text_size) {
7371
fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
7472
(long) n, (long) text_size);
75-
free(text);
76-
return 1;
73+
goto error;
7774
}
7875
text[text_size] = '\0';
7976

@@ -87,11 +84,13 @@ main(int argc, char *argv[])
8784

8885
code = Py_CompileStringExFlags(text, "<frozen importlib._bootstrap>",
8986
Py_file_input, NULL, 0);
90-
free(text);
9187
if (code == NULL)
9288
goto error;
89+
free(text);
90+
text = NULL;
91+
9392
marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
94-
Py_DECREF(code);
93+
Py_CLEAR(code);
9594
if (marshalled == NULL)
9695
goto error;
9796

@@ -104,8 +103,7 @@ main(int argc, char *argv[])
104103
outfile = fopen(outpath, "w");
105104
if (outfile == NULL) {
106105
fprintf(stderr, "cannot open '%s' for writing\n", outpath);
107-
Py_DECREF(marshalled);
108-
return 1;
106+
goto error;
109107
}
110108
fprintf(outfile, "%s\n", header);
111109
fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
@@ -119,16 +117,13 @@ main(int argc, char *argv[])
119117
}
120118
fprintf(outfile, "};\n");
121119

122-
Py_DECREF(marshalled);
120+
Py_CLEAR(marshalled);
123121

124122
Py_Finalize();
125-
if (infile)
126-
fclose(infile);
127123
if (outfile) {
128124
if (ferror(outfile)) {
129125
fprintf(stderr, "error when writing to '%s'\n", outpath);
130-
fclose(outfile);
131-
return 1;
126+
goto error;
132127
}
133128
fclose(outfile);
134129
}
@@ -141,5 +136,9 @@ main(int argc, char *argv[])
141136
fclose(infile);
142137
if (outfile)
143138
fclose(outfile);
139+
if (text)
140+
free(text);
141+
if (marshalled)
142+
Py_DECREF(marshalled);
144143
return 1;
145144
}

0 commit comments

Comments
 (0)