Skip to content

Commit 91411b6

Browse files
author
Michael Meskes
committed
Fixed one memory leak in descriptor code.
Made sure ecpg deletes output file in case of an error.
1 parent 5f2bda1 commit 91411b6

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

src/interfaces/ecpg/ecpglib/descriptor.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* dynamic SQL support routines
22
*
3-
* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.21 2007/04/27 06:56:11 meskes Exp $
3+
* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.22 2007/06/11 11:52:08 meskes Exp $
44
*/
55

66
#define POSTGRES_ECPG_INTERNAL
@@ -547,7 +547,7 @@ ECPGset_desc(int lineno, const char *desc_name, int index,...)
547547
ECPGfree(var);
548548
return false;
549549
}
550-
550+
ECPGfree(desc_item->data); /* free() takes care of a potential NULL value */
551551
desc_item->data = (char *) tobeinserted;
552552
tobeinserted = NULL;
553553
break;
@@ -607,6 +607,18 @@ ECPGdeallocate_desc(int line, const char *name)
607607
{
608608
if (!strcmp(name, i->name))
609609
{
610+
struct descriptor_item *desc_item;
611+
612+
for (desc_item = i->items; desc_item;)
613+
{
614+
struct descriptor_item *di;
615+
616+
ECPGfree(desc_item->data);
617+
di = desc_item;
618+
desc_item = desc_item->next;
619+
ECPGfree(di);
620+
}
621+
610622
*lastptr = i->next;
611623
ECPGfree(i->name);
612624
PQclear(i->result);

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.66 2007/04/27 06:56:11 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.67 2007/06/11 11:52:08 meskes Exp $ */
22

33
/*
44
* The aim is to get a simpler inteface to the database routines.
@@ -48,7 +48,7 @@ quote_postgres(char *arg, bool quote, int lineno)
4848
* will be quoted once they are inserted in a statement
4949
*/
5050
if (!quote)
51-
return res = ECPGstrdup(arg, lineno);
51+
return arg;
5252
else
5353
{
5454
length = strlen(arg);

src/interfaces/ecpg/preproc/ecpg.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.98 2007/03/17 19:25:23 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.99 2007/06/11 11:52:08 meskes Exp $ */
22

33
/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
44
/* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */
@@ -20,6 +20,8 @@ int ret_value = 0,
2020
header_mode = false,
2121
regression_mode = false;
2222

23+
char *output_filename;
24+
2325
enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL;
2426

2527
struct _include_path *include_paths = NULL;
@@ -135,6 +137,7 @@ main(int argc, char *const argv[])
135137

136138
find_my_exec(argv[0], my_exec_path);
137139

140+
output_filename = NULL;
138141
while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
139142
{
140143
switch (c)
@@ -163,14 +166,18 @@ main(int argc, char *const argv[])
163166
regression_mode = true;
164167
break;
165168
case 'o':
166-
if (strcmp(optarg, "-") == 0)
169+
output_filename = optarg;
170+
if (strcmp(output_filename, "-") == 0)
167171
yyout = stdout;
168172
else
169-
yyout = fopen(optarg, PG_BINARY_W);
173+
yyout = fopen(output_filename, PG_BINARY_W);
170174

171-
if (yyout == NULL)
175+
if (yyout == NULL)
176+
{
172177
fprintf(stderr, "%s: could not open file \"%s\": %s\n",
173-
progname, optarg, strerror(errno));
178+
progname, output_filename, strerror(errno));
179+
output_filename = NULL;
180+
}
174181
else
175182
out_option = 1;
176183
break;
@@ -269,8 +276,7 @@ main(int argc, char *const argv[])
269276
/* after the options there must not be anything but filenames */
270277
for (fnr = optind; fnr < argc; fnr++)
271278
{
272-
char *output_filename = NULL,
273-
*ptr2ext;
279+
char *ptr2ext;
274280

275281
/* If argv[fnr] is "-" we have to read from stdin */
276282
if (strcmp(argv[fnr], "-") == 0)
@@ -467,7 +473,7 @@ main(int argc, char *const argv[])
467473
fclose(yyout);
468474
}
469475

470-
if (output_filename)
476+
if (output_filename && out_option == 0)
471477
free(output_filename);
472478

473479
free(input_filename);

src/interfaces/ecpg/preproc/extern.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/extern.h,v 1.65 2007/03/17 19:25:23 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/extern.h,v 1.66 2007/06/11 11:52:08 meskes Exp $ */
22

33
#ifndef _ECPG_PREPROC_EXTERN_H
44
#define _ECPG_PREPROC_EXTERN_H
@@ -37,6 +37,7 @@ extern int yylineno,
3737
yyleng;
3838
extern FILE *yyin,
3939
*yyout;
40+
extern char *output_filename;
4041

4142
extern struct _include_path *include_paths;
4243
extern struct cursor *cur;
@@ -93,7 +94,7 @@ extern ScanKeyword *ScanKeywordLookup(char *text);
9394
extern void scanner_init(const char *);
9495
extern void parser_init(void);
9596
extern void scanner_finish(void);
96-
int filtered_base_yylex(void);
97+
extern int filtered_base_yylex(void);
9798

9899
/* return codes */
99100

src/interfaces/ecpg/preproc/preproc.y

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.343 2007/05/10 09:53:17 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.344 2007/06/11 11:52:08 meskes Exp $ */
22

33
/* Copyright comment */
44
%{
@@ -99,6 +99,10 @@ mmerror(int error_code, enum errortype type, char * error, ...)
9999
ret_value = error_code;
100100
break;
101101
case ET_FATAL:
102+
fclose(yyin);
103+
fclose(yyout);
104+
if (unlink(output_filename) != 0)
105+
fprintf(stderr, "Could not remove ourput file %s!\n", output_filename);
102106
exit(error_code);
103107
}
104108
}

0 commit comments

Comments
 (0)