From 43d82df406114223c70961f0774d622536a0629d Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Sun, 21 Jul 2013 23:05:04 +0200 Subject: [PATCH] Now all error paths of _freeze_importlib use 'goto error' and the error label cleans up all used resources. --- Modules/_freeze_importlib.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/Modules/_freeze_importlib.c b/Modules/_freeze_importlib.c index b773c32537b..8f07a89a8af 100644 --- a/Modules/_freeze_importlib.c +++ b/Modules/_freeze_importlib.c @@ -34,12 +34,12 @@ int main(int argc, char *argv[]) { char *inpath, *outpath; - FILE *infile, *outfile = NULL; + FILE *infile = NULL, *outfile = NULL; struct stat st; size_t text_size, data_size, n; - char *text; + char *text = NULL; unsigned char *data; - PyObject *code, *marshalled; + PyObject *code = NULL, *marshalled = NULL; PyImport_FrozenModules = _PyImport_FrozenModules; @@ -52,19 +52,17 @@ main(int argc, char *argv[]) infile = fopen(inpath, "rb"); if (infile == NULL) { fprintf(stderr, "cannot open '%s' for reading\n", inpath); - return 1; + goto error; } if (fstat(fileno(infile), &st)) { - fclose(infile); fprintf(stderr, "cannot fstat '%s'\n", inpath); - return 1; + goto error; } text_size = st.st_size; text = (char *) malloc(text_size + 1); if (text == NULL) { - fclose(infile); fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size); - return 1; + goto error; } n = fread(text, 1, text_size, infile); fclose(infile); @@ -72,8 +70,7 @@ main(int argc, char *argv[]) if (n < text_size) { fprintf(stderr, "read too short: got %ld instead of %ld bytes\n", (long) n, (long) text_size); - free(text); - return 1; + goto error; } text[text_size] = '\0'; @@ -87,11 +84,13 @@ main(int argc, char *argv[]) code = Py_CompileStringExFlags(text, "", Py_file_input, NULL, 0); - free(text); if (code == NULL) goto error; + free(text); + text = NULL; + marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION); - Py_DECREF(code); + Py_CLEAR(code); if (marshalled == NULL) goto error; @@ -104,8 +103,7 @@ main(int argc, char *argv[]) outfile = fopen(outpath, "w"); if (outfile == NULL) { fprintf(stderr, "cannot open '%s' for writing\n", outpath); - Py_DECREF(marshalled); - return 1; + goto error; } fprintf(outfile, "%s\n", header); fprintf(outfile, "unsigned char _Py_M__importlib[] = {\n"); @@ -119,16 +117,13 @@ main(int argc, char *argv[]) } fprintf(outfile, "};\n"); - Py_DECREF(marshalled); + Py_CLEAR(marshalled); Py_Finalize(); - if (infile) - fclose(infile); if (outfile) { if (ferror(outfile)) { fprintf(stderr, "error when writing to '%s'\n", outpath); - fclose(outfile); - return 1; + goto error; } fclose(outfile); } @@ -141,5 +136,9 @@ main(int argc, char *argv[]) fclose(infile); if (outfile) fclose(outfile); + if (text) + free(text); + if (marshalled) + Py_DECREF(marshalled); return 1; }