diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-09-02 20:37:55 +0000 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-09-02 20:37:55 +0000 |
| commit | fbb2b69c8f478c2910a32e25d80eaf67d0dd3cbf (patch) | |
| tree | b1533d094abd1c506210616b6b77d50cf8b8c812 /src | |
| parent | dd6edd5efd042f7aaaccb7606fb5f477c9dc9888 (diff) | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/backend/bootstrap/bootparse.y | 13 | ||||
| -rw-r--r-- | src/backend/parser/gram.y | 13 | ||||
| -rw-r--r-- | src/pl/plpgsql/src/gram.y | 14 |
3 files changed, 37 insertions, 3 deletions
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y index 8019b244ae8..9997566002e 100644 --- a/src/backend/bootstrap/bootparse.y +++ b/src/backend/bootstrap/bootparse.y @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.93 2008/09/01 20:42:43 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.94 2008/09/02 20:37:54 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -54,6 +54,17 @@ #define atooid(x) ((Oid) strtoul((x), NULL, 10)) +/* + * Bison doesn't allocate anything that needs to live across parser calls, + * so we can easily have it use palloc instead of malloc. This prevents + * memory leaks if we error out during parsing. Note this only works with + * bison >= 2.0. However, in bison 1.875 the default is to use alloca() + * if possible, so there's not really much problem anyhow, at least if + * you're building with gcc. + */ +#define YYMALLOC palloc +#define YYFREE pfree + static void do_start(void) { diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 26a3198d37b..5c716dd8f22 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -11,7 +11,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.621 2008/09/01 20:42:44 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.622 2008/09/02 20:37:54 tgl Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -79,6 +79,17 @@ */ #define base_yylex filtered_base_yylex +/* + * Bison doesn't allocate anything that needs to live across parser calls, + * so we can easily have it use palloc instead of malloc. This prevents + * memory leaks if we error out during parsing. Note this only works with + * bison >= 2.0. However, in bison 1.875 the default is to use alloca() + * if possible, so there's not really much problem anyhow, at least if + * you're building with gcc. + */ +#define YYMALLOC palloc +#define YYFREE pfree + extern List *parsetree; /* final parse result is delivered here */ static bool QueryIsRule = FALSE; diff --git a/src/pl/plpgsql/src/gram.y b/src/pl/plpgsql/src/gram.y index dfd37f67717..cf3c751cc0b 100644 --- a/src/pl/plpgsql/src/gram.y +++ b/src/pl/plpgsql/src/gram.y @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.113 2008/05/15 22:39:49 tgl Exp $ + * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.114 2008/09/02 20:37:55 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -19,6 +19,18 @@ #include "parser/parser.h" +/* + * Bison doesn't allocate anything that needs to live across parser calls, + * so we can easily have it use palloc instead of malloc. This prevents + * memory leaks if we error out during parsing. Note this only works with + * bison >= 2.0. However, in bison 1.875 the default is to use alloca() + * if possible, so there's not really much problem anyhow, at least if + * you're building with gcc. + */ +#define YYMALLOC palloc +#define YYFREE pfree + + static PLpgSQL_expr *read_sql_construct(int until, int until2, int until3, |
