Skip to content

Commit 802fe92

Browse files
committed
cube: pure parser and reentrant scanner
Use the flex %option reentrant and the bison option %pure-parser to make the generated scanner and parser pure, reentrant, and thread-safe. 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. Simplify flex scan buffer management: Instead of constructing the buffer from pieces and then using yy_scan_buffer(), we can just use yy_scan_string(), which does the same thing internally. (Actually, we use yy_scan_bytes() here because we already have the length.) The previous code was necessary because we allocated the buffer with palloc() and the rest of the state was handled by malloc(). But this is no longer the case; everything is under palloc() now. (We could even get rid of the yylex_destroy() call and just let the memory context cleanup handle everything. But for now, we preserve the existing behavior.) Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Andreas Karlsson <andreas@proxel.se> Discussion: https://www.postgresql.org/message-id/flat/eb6faeac-2a8a-4b69-9189-c33c520e5b7b@eisentraut.org
1 parent 477728b commit 802fe92

File tree

4 files changed

+74
-52
lines changed

4 files changed

+74
-52
lines changed

contrib/cube/cube.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ cube_in(PG_FUNCTION_ARGS)
120120
char *str = PG_GETARG_CSTRING(0);
121121
NDBOX *result;
122122
Size scanbuflen;
123+
yyscan_t scanner;
123124

124-
cube_scanner_init(str, &scanbuflen);
125+
cube_scanner_init(str, &scanbuflen, &scanner);
125126

126-
cube_yyparse(&result, scanbuflen, fcinfo->context);
127+
cube_yyparse(&result, scanbuflen, fcinfo->context, scanner);
127128

128129
/* We might as well run this even on failure. */
129-
cube_scanner_finish();
130+
cube_scanner_finish(scanner);
130131

131132
PG_RETURN_NDBOX_P(result);
132133
}

contrib/cube/cubedata.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,21 @@ typedef struct NDBOX
5959
#define CubeKNNDistanceEuclid 17 /* <-> */
6060
#define CubeKNNDistanceChebyshev 18 /* <=> */
6161

62+
/* for cubescan.l and cubeparse.y */
63+
/* All grammar constructs return strings */
64+
#define YYSTYPE char *
65+
typedef void *yyscan_t;
66+
6267
/* in cubescan.l */
63-
extern int cube_yylex(void);
68+
extern int cube_yylex(YYSTYPE *yylval_param, yyscan_t yyscanner);
6469
extern void cube_yyerror(NDBOX **result, Size scanbuflen,
6570
struct Node *escontext,
71+
yyscan_t yyscanner,
6672
const char *message);
67-
extern void cube_scanner_init(const char *str, Size *scanbuflen);
68-
extern void cube_scanner_finish(void);
73+
extern void cube_scanner_init(const char *str, Size *scanbuflen, yyscan_t *yyscannerp);
74+
extern void cube_scanner_finish(yyscan_t yyscanner);
6975

7076
/* in cubeparse.y */
7177
extern int cube_yyparse(NDBOX **result, Size scanbuflen,
72-
struct Node *escontext);
78+
struct Node *escontext,
79+
yyscan_t yyscanner);

contrib/cube/cubeparse.y

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77
#include "postgres.h"
88

99
#include "cubedata.h"
10+
#include "cubeparse.h" /* must be after cubedata.h for YYSTYPE and NDBOX */
1011
#include "nodes/miscnodes.h"
1112
#include "utils/float.h"
1213
#include "varatt.h"
1314

14-
/* All grammar constructs return strings */
15-
#define YYSTYPE char *
16-
17-
#include "cubeparse.h"
18-
19-
/* silence -Wmissing-variable-declarations */
20-
extern int cube_yychar;
21-
extern int cube_yynerrs;
22-
2315
/*
2416
* Bison doesn't allocate anything that needs to live across parser calls,
2517
* so we can easily have it use palloc instead of malloc. This prevents
@@ -40,6 +32,9 @@ static bool write_point_as_box(int dim, char *str,
4032
%parse-param {NDBOX **result}
4133
%parse-param {Size scanbuflen}
4234
%parse-param {struct Node *escontext}
35+
%parse-param {yyscan_t yyscanner}
36+
%lex-param {yyscan_t yyscanner}
37+
%pure-parser
4338
%expect 0
4439
%name-prefix="cube_yy"
4540

@@ -75,6 +70,8 @@ box: O_BRACKET paren_list COMMA paren_list C_BRACKET
7570

7671
if (!write_box(dim, $2, $4, result, escontext))
7772
YYABORT;
73+
74+
(void) yynerrs; /* suppress compiler warning */
7875
}
7976

8077
| paren_list COMMA paren_list

contrib/cube/cubescan.l

+53-36
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66

77
#include "postgres.h"
88

9-
/*
10-
* NB: include cubeparse.h only AFTER defining YYSTYPE (to match cubeparse.y)
11-
* and cubedata.h for NDBOX.
12-
*/
139
#include "cubedata.h"
14-
#define YYSTYPE char *
15-
#include "cubeparse.h"
10+
#include "cubeparse.h" /* must be after cubedata.h for YYSTYPE and NDBOX */
1611
}
1712

1813
%{
@@ -30,18 +25,19 @@ fprintf_to_ereport(const char *fmt, const char *msg)
3025
{
3126
ereport(ERROR, (errmsg_internal("%s", msg)));
3227
}
33-
34-
/* Handles to the buffer that the lexer uses internally */
35-
static YY_BUFFER_STATE scanbufhandle;
36-
static char *scanbuf;
3728
%}
3829

30+
%option reentrant
31+
%option bison-bridge
3932
%option 8bit
4033
%option never-interactive
4134
%option nodefault
4235
%option noinput
4336
%option nounput
4437
%option noyywrap
38+
%option noyyalloc
39+
%option noyyrealloc
40+
%option noyyfree
4541
%option warn
4642
%option prefix="cube_yy"
4743

@@ -55,14 +51,14 @@ NaN [nN][aA][nN]
5551

5652
%%
5753

58-
{float} cube_yylval = yytext; return CUBEFLOAT;
59-
{infinity} cube_yylval = yytext; return CUBEFLOAT;
60-
{NaN} cube_yylval = yytext; return CUBEFLOAT;
61-
\[ cube_yylval = "("; return O_BRACKET;
62-
\] cube_yylval = ")"; return C_BRACKET;
63-
\( cube_yylval = "("; return O_PAREN;
64-
\) cube_yylval = ")"; return C_PAREN;
65-
\, cube_yylval = ","; return COMMA;
54+
{float} *yylval = yytext; return CUBEFLOAT;
55+
{infinity} *yylval = yytext; return CUBEFLOAT;
56+
{NaN} *yylval = yytext; return CUBEFLOAT;
57+
\[ *yylval = "("; return O_BRACKET;
58+
\] *yylval = ")"; return C_BRACKET;
59+
\( *yylval = "("; return O_PAREN;
60+
\) *yylval = ")"; return C_PAREN;
61+
\, *yylval = ","; return COMMA;
6662
[ \t\n\r\f\v]+ /* discard spaces */
6763
. return yytext[0]; /* alert parser of the garbage */
6864

@@ -74,8 +70,11 @@ NaN [nN][aA][nN]
7470
void
7571
cube_yyerror(NDBOX **result, Size scanbuflen,
7672
struct Node *escontext,
73+
yyscan_t yyscanner,
7774
const char *message)
7875
{
76+
struct yyguts_t * yyg = (struct yyguts_t *) yyscanner; /* needed for yytext macro */
77+
7978
if (*yytext == YY_END_OF_BUFFER_CHAR)
8079
{
8180
errsave(escontext,
@@ -99,35 +98,53 @@ cube_yyerror(NDBOX **result, Size scanbuflen,
9998
* Called before any actual parsing is done
10099
*/
101100
void
102-
cube_scanner_init(const char *str, Size *scanbuflen)
101+
cube_scanner_init(const char *str, Size *scanbuflen, yyscan_t *yyscannerp)
103102
{
104103
Size slen = strlen(str);
104+
yyscan_t yyscanner;
105105

106-
/*
107-
* Might be left over after ereport()
108-
*/
109-
if (YY_CURRENT_BUFFER)
110-
yy_delete_buffer(YY_CURRENT_BUFFER);
106+
if (yylex_init(yyscannerp) != 0)
107+
elog(ERROR, "yylex_init() failed: %m");
111108

112-
/*
113-
* Make a scan buffer with special termination needed by flex.
114-
*/
115-
*scanbuflen = slen;
116-
scanbuf = palloc(slen + 2);
117-
memcpy(scanbuf, str, slen);
118-
scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
119-
scanbufhandle = yy_scan_buffer(scanbuf, slen + 2);
109+
yyscanner = *yyscannerp;
120110

121-
BEGIN(INITIAL);
111+
yy_scan_bytes(str, slen, yyscanner);
112+
*scanbuflen = slen;
122113
}
123114

124115

125116
/*
126117
* Called after parsing is done to clean up after cube_scanner_init()
127118
*/
128119
void
129-
cube_scanner_finish(void)
120+
cube_scanner_finish(yyscan_t yyscanner)
121+
{
122+
yylex_destroy(yyscanner);
123+
}
124+
125+
/*
126+
* Interface functions to make flex use palloc() instead of malloc().
127+
* It'd be better to make these static, but flex insists otherwise.
128+
*/
129+
130+
void *
131+
yyalloc(yy_size_t size, yyscan_t yyscanner)
132+
{
133+
return palloc(size);
134+
}
135+
136+
void *
137+
yyrealloc(void *ptr, yy_size_t size, yyscan_t yyscanner)
138+
{
139+
if (ptr)
140+
return repalloc(ptr, size);
141+
else
142+
return palloc(size);
143+
}
144+
145+
void
146+
yyfree(void *ptr, yyscan_t yyscanner)
130147
{
131-
yy_delete_buffer(scanbufhandle);
132-
pfree(scanbuf);
148+
if (ptr)
149+
pfree(ptr);
133150
}

0 commit comments

Comments
 (0)