v0-0002-cube-Use-palloc-instead-of-malloc-for-flex.patch

text/plain

Filename: v0-0002-cube-Use-palloc-instead-of-malloc-for-flex.patch
Type: text/plain
Part: 1
Message: pure parsers and reentrant scanners

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v0-0002
Subject: cube: Use palloc() instead of malloc() for flex
File+
contrib/cube/cubescan.l 30 0
From f092720cf73b62194d10ddf65948d03bbaafb04b Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 2 Dec 2024 10:35:37 +0100
Subject: [PATCH v0 02/15] cube: Use palloc() instead of malloc() for flex

Make the generated scanner use palloc() etc. instead of malloc() etc.
Previously, we only used palloc() for the buffer, but flex would still
use malloc() for its internal structures.  As a result, there could be
some small memory leaks in case of uncaught errors.  (We do catch
normal syntax errors as soft errors.)  Now, all the memory is under
palloc() control, so there are no more such issues.
---
 contrib/cube/cubescan.l | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/contrib/cube/cubescan.l b/contrib/cube/cubescan.l
index 09109675711..b741e77d7e1 100644
--- a/contrib/cube/cubescan.l
+++ b/contrib/cube/cubescan.l
@@ -38,6 +38,9 @@ static char *scanbuf; // FIXME
 %option noinput
 %option nounput
 %option noyywrap
+%option noyyalloc
+%option noyyrealloc
+%option noyyfree
 %option warn
 %option prefix="cube_yy"
 
@@ -128,3 +131,30 @@ cube_scanner_finish(yyscan_t yyscanner)
 	yylex_destroy(yyscanner);
 	pfree(scanbuf);
 }
+
+/*
+ * Interface functions to make flex use palloc() instead of malloc().
+ * It'd be better to make these static, but flex insists otherwise.
+ */
+
+void *
+yyalloc(yy_size_t size, yyscan_t yyscanner)
+{
+	return palloc(size);
+}
+
+void *
+yyrealloc(void *ptr, yy_size_t size, yyscan_t yyscanner)
+{
+	if (ptr)
+		return repalloc(ptr, size);
+	else
+		return palloc(size);
+}
+
+void
+yyfree(void *ptr, yyscan_t yyscanner)
+{
+	if (ptr)
+		pfree(ptr);
+}
-- 
2.47.1