summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2008-09-02 20:37:55 +0000
committerTom Lane2008-09-02 20:37:55 +0000
commitae1958a82656508f24cf938dee1842d48cc49b8f (patch)
treef18a51d7983235097f76da0ecc024f90fccc0593
parent0df74714a0282c055b94b62084cec64bd550c5ef (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.
-rw-r--r--contrib/cube/cubeparse.y11
-rw-r--r--contrib/seg/segparse.y11
-rw-r--r--src/backend/bootstrap/bootparse.y11
-rw-r--r--src/backend/parser/gram.y11
-rw-r--r--src/pl/plpgsql/src/gram.y12
5 files changed, 56 insertions, 0 deletions
diff --git a/contrib/cube/cubeparse.y b/contrib/cube/cubeparse.y
index 26a97810fa..6b96da5127 100644
--- a/contrib/cube/cubeparse.y
+++ b/contrib/cube/cubeparse.y
@@ -12,6 +12,17 @@
#include "cubedata.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
+
extern int cube_yylex(void);
static char *scanbuf;
diff --git a/contrib/seg/segparse.y b/contrib/seg/segparse.y
index dc56fb580f..47e3d398aa 100644
--- a/contrib/seg/segparse.y
+++ b/contrib/seg/segparse.y
@@ -9,6 +9,17 @@
#include "utils/builtins.h"
#include "segdata.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
+
extern int seg_yylex(void);
extern int significant_digits(char *str); /* defined in seg.c */
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index e89f718b8e..38971e0d98 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -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 0b70ba007c..e9b45a400e 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -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 075d66367a..e6f806b066 100644
--- a/src/pl/plpgsql/src/gram.y
+++ b/src/pl/plpgsql/src/gram.y
@@ -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,