Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fbb2b69

Browse files
committed
Prevent memory leaks in our various bison parsers when an error occurs
during parsing. Formerly the parser's stack was allocated with malloc and so wouldn't be reclaimed; this patch makes it use palloc instead, so that flushing the current context will reclaim the memory. Per Marko Kreen.
1 parent dd6edd5 commit fbb2b69

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

contrib/cube/cubeparse.y

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* NdBox = [(lowerleft),(upperright)] */
33
/* [(xLL(1)...xLL(N)),(xUR(1)...xUR(n))] */
44

5-
/* $PostgreSQL: pgsql/contrib/cube/cubeparse.y,v 1.17 2007/02/27 23:48:05 tgl Exp $ */
5+
/* $PostgreSQL: pgsql/contrib/cube/cubeparse.y,v 1.18 2008/09/02 20:37:54 tgl Exp $ */
66

77
#define YYPARSE_PARAM result /* need this to pass a pointer (void *) to yyparse */
88
#define YYSTYPE char *
@@ -12,6 +12,17 @@
1212

1313
#include "cubedata.h"
1414

15+
/*
16+
* Bison doesn't allocate anything that needs to live across parser calls,
17+
* so we can easily have it use palloc instead of malloc. This prevents
18+
* memory leaks if we error out during parsing. Note this only works with
19+
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
20+
* if possible, so there's not really much problem anyhow, at least if
21+
* you're building with gcc.
22+
*/
23+
#define YYMALLOC palloc
24+
#define YYFREE pfree
25+
1526
extern int cube_yylex(void);
1627

1728
static char *scanbuf;

contrib/seg/segparse.y

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
#include "utils/builtins.h"
1010
#include "segdata.h"
1111

12+
/*
13+
* Bison doesn't allocate anything that needs to live across parser calls,
14+
* so we can easily have it use palloc instead of malloc. This prevents
15+
* memory leaks if we error out during parsing. Note this only works with
16+
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
17+
* if possible, so there's not really much problem anyhow, at least if
18+
* you're building with gcc.
19+
*/
20+
#define YYMALLOC palloc
21+
#define YYFREE pfree
22+
1223
extern int seg_yylex(void);
1324

1425
extern int significant_digits(char *str); /* defined in seg.c */

src/backend/bootstrap/bootparse.y

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.93 2008/09/01 20:42:43 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.94 2008/09/02 20:37:54 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -54,6 +54,17 @@
5454
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
5555

5656

57+
/*
58+
* Bison doesn't allocate anything that needs to live across parser calls,
59+
* so we can easily have it use palloc instead of malloc. This prevents
60+
* memory leaks if we error out during parsing. Note this only works with
61+
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
62+
* if possible, so there's not really much problem anyhow, at least if
63+
* you're building with gcc.
64+
*/
65+
#define YYMALLOC palloc
66+
#define YYFREE pfree
67+
5768
static void
5869
do_start(void)
5970
{

src/backend/parser/gram.y

+12-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.621 2008/09/01 20:42:44 tgl Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.622 2008/09/02 20:37:54 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -79,6 +79,17 @@
7979
*/
8080
#define base_yylex filtered_base_yylex
8181

82+
/*
83+
* Bison doesn't allocate anything that needs to live across parser calls,
84+
* so we can easily have it use palloc instead of malloc. This prevents
85+
* memory leaks if we error out during parsing. Note this only works with
86+
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
87+
* if possible, so there's not really much problem anyhow, at least if
88+
* you're building with gcc.
89+
*/
90+
#define YYMALLOC palloc
91+
#define YYFREE pfree
92+
8293
extern List *parsetree; /* final parse result is delivered here */
8394

8495
static bool QueryIsRule = FALSE;

src/pl/plpgsql/src/gram.y

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.113 2008/05/15 22:39:49 tgl Exp $
12+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.114 2008/09/02 20:37:55 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -19,6 +19,18 @@
1919
#include "parser/parser.h"
2020

2121

22+
/*
23+
* Bison doesn't allocate anything that needs to live across parser calls,
24+
* so we can easily have it use palloc instead of malloc. This prevents
25+
* memory leaks if we error out during parsing. Note this only works with
26+
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
27+
* if possible, so there's not really much problem anyhow, at least if
28+
* you're building with gcc.
29+
*/
30+
#define YYMALLOC palloc
31+
#define YYFREE pfree
32+
33+
2234
static PLpgSQL_expr *read_sql_construct(int until,
2335
int until2,
2436
int until3,

0 commit comments

Comments
 (0)