Prevent memory leaks in our various bison parsers when an error occurs
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 Sep 2008 20:37:55 +0000 (20:37 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 Sep 2008 20:37:55 +0000 (20:37 +0000)
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.

contrib/cube/cubeparse.y
contrib/seg/segparse.y
src/backend/bootstrap/bootparse.y
src/backend/parser/gram.y
src/pl/plpgsql/src/gram.y

index 26a97810fae11c74797daeb7777f2ba8f1d905ad..6b96da5127f887d3f62a046a19674441de8ef8af 100644 (file)
 
 #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;
index dc56fb580feecdd05b69d3405ae95bdde4a35e5b..47e3d398aaef24af015a06d2e41377aaa6977615 100644 (file)
@@ -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 */
index e89f718b8eb2a62609cd4c9b1cf5d7aee1cd99a5..38971e0d985a5096fe2cda5fba62c02a317d9f27 100644 (file)
 #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)
 {
index 0b70ba007c3c287419db95a3bd55ef20890434fa..e9b45a400e20b2c2cd7a7c7becd8d2bca975051e 100644 (file)
  */
 #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;
index 075d66367ad150a2377dde5dad0febbb8dde211f..e6f806b066b3bcdfc0736017800b1fe66cb96ddc 100644 (file)
 #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,