Fixed one memory leak in descriptor code.
authorMichael Meskes <meskes@postgresql.org>
Mon, 11 Jun 2007 11:52:08 +0000 (11:52 +0000)
committerMichael Meskes <meskes@postgresql.org>
Mon, 11 Jun 2007 11:52:08 +0000 (11:52 +0000)
Made sure ecpg deletes output file in case of an error.

src/interfaces/ecpg/ecpglib/descriptor.c
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/ecpg/preproc/ecpg.c
src/interfaces/ecpg/preproc/extern.h
src/interfaces/ecpg/preproc/preproc.y

index 1810191484fa45d2a82941b05ef84c76ebd9c643..ac19070c7c2288ba4cd1a72d15fe66ee25f7e4cd 100644 (file)
@@ -547,7 +547,7 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...)
                                                ECPGfree(var);
                                                return false;
                                        }
-
+                                       ECPGfree(desc_item->data); /* free() takes care of a potential NULL value */
                                        desc_item->data = (char *) tobeinserted;
                                        tobeinserted = NULL;
                                        break;
@@ -607,6 +607,18 @@ ECPGdeallocate_desc(int line, const char *name)
        {
                if (!strcmp(name, i->name))
                {
+                       struct descriptor_item *desc_item;
+
+                       for (desc_item = i->items; desc_item;)
+                       {
+                               struct descriptor_item *di;
+
+                               ECPGfree(desc_item->data);
+                               di = desc_item;
+                               desc_item = desc_item->next;
+                               ECPGfree(di);
+                       }
+
                        *lastptr = i->next;
                        ECPGfree(i->name);
                        PQclear(i->result);
index 35940120b38b4f9530b497593197027a979a3d9f..aa46196102d8affd371c10372b8deab92e9e8751 100644 (file)
@@ -48,7 +48,7 @@ quote_postgres(char *arg, bool quote, int lineno)
         * will be quoted once they are inserted in a statement
         */
        if (!quote)
-               return res = ECPGstrdup(arg, lineno);
+               return arg;
        else
        {
                length = strlen(arg);
index 9bccb355149ac1310192bb5692fd54c7f2879695..b6e731d63cf3c264411528527b821974dc8790a3 100644 (file)
@@ -20,6 +20,8 @@ int                   ret_value = 0,
                        header_mode = false,
                        regression_mode = false;
 
+char      *output_filename;
+
 enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
 
 struct _include_path *include_paths = NULL;
@@ -135,6 +137,7 @@ main(int argc, char *const argv[])
 
        find_my_exec(argv[0], my_exec_path);
 
+       output_filename = NULL;
        while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
        {
                switch (c)
@@ -163,14 +166,18 @@ main(int argc, char *const argv[])
                                regression_mode = true;
                                break;
                        case 'o':
-                               if (strcmp(optarg, "-") == 0)
+                               output_filename = optarg;
+                               if (strcmp(output_filename, "-") == 0)
                                        yyout = stdout;
                                else
-                                       yyout = fopen(optarg, PG_BINARY_W);
+                                       yyout = fopen(output_filename, PG_BINARY_W);
 
-                               if (yyout == NULL)
+                               if (yyout == NULL) 
+                               {
                                        fprintf(stderr, "%s: could not open file \"%s\": %s\n",
-                                                       progname, optarg, strerror(errno));
+                                                       progname, output_filename, strerror(errno));
+                                       output_filename = NULL;
+                               }
                                else
                                        out_option = 1;
                                break;
@@ -269,8 +276,7 @@ main(int argc, char *const argv[])
                /* after the options there must not be anything but filenames */
                for (fnr = optind; fnr < argc; fnr++)
                {
-                       char       *output_filename = NULL,
-                                          *ptr2ext;
+                       char *ptr2ext;
 
                        /* If argv[fnr] is "-" we have to read from stdin */
                        if (strcmp(argv[fnr], "-") == 0)
@@ -467,7 +473,7 @@ main(int argc, char *const argv[])
                                        fclose(yyout);
                        }
 
-                       if (output_filename)
+                       if (output_filename && out_option == 0)
                                free(output_filename);
 
                        free(input_filename);
index e949de1691b3bd8a333b1c92a854817ef817fbde..f3e777d532dd31638528fc8f24a2ae4dfa9a4635 100644 (file)
@@ -37,6 +37,7 @@ extern int    yylineno,
                        yyleng;
 extern FILE *yyin,
                   *yyout;
+extern char *output_filename;
 
 extern struct _include_path *include_paths;
 extern struct cursor *cur;
@@ -93,7 +94,7 @@ extern ScanKeyword *ScanKeywordLookup(char *text);
 extern void scanner_init(const char *);
 extern void parser_init(void);
 extern void scanner_finish(void);
-int filtered_base_yylex(void);
+extern int filtered_base_yylex(void);
 
 /* return codes */
 
index f94a275d0c1823dcce2a5a5666f46c6f78743ead..45a8359f1a70fb9d8a5be7c93348a581536b912f 100644 (file)
@@ -99,6 +99,10 @@ mmerror(int error_code, enum errortype type, char * error, ...)
                        ret_value = error_code;
                        break;
                case ET_FATAL:
+                       fclose(yyin);
+                       fclose(yyout);
+                       if (unlink(output_filename) != 0)
+                               fprintf(stderr, "Could not remove ourput file %s!\n", output_filename);
                        exit(error_code);
        }
 }