Skip to content

Commit 975e788

Browse files
committed
Fix Issue 2256: segmentation fault when calling coalesce function
Fixed issue 2256: A segmentation fault occurs when calling the coalesce function in PostgreSQL version 17. This likely predates 17 and includes other similar types of "functions". See issues 1124 (PR 1125) and 1303 (PR 1317) for more details. This issue is due to coalesce() being processed differently from other functions. Additionally, greatest() was found to exhibit the same behavior. They were added to the list of types to ignore during the cypher analyze phase. A few others were added: CaseExpr, XmlExpr, ArrayExpr, & RowExpr. Although, I wasn't able to find cases where these caused crashes. Added regression tests. modified: regress/expected/cypher.out modified: regress/sql/cypher.sql modified: src/backend/parser/cypher_analyze.c
1 parent 5aed9ec commit 975e788

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

regress/expected/cypher.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ CREATE TABLE my_edges AS
169169
-- create a table of 4 columns, u, e, v, p. should be 5 rows
170170
CREATE TABLE my_detailed_paths AS
171171
(SELECT * FROM cypher('issue_1767', $$ MATCH p=(u)-[e]->(v) RETURN u,e,v,p $$) as (u agtype, e agtype, v agtype, p agtype));
172+
--
173+
-- Issue 2256: A segmentation fault occurs when calling the coalesce function
174+
-- This also occurs with the greatest function too.
175+
--
176+
SELECT * FROM coalesce(1, 0);
177+
coalesce
178+
----------
179+
1
180+
(1 row)
181+
182+
SELECT * FROM greatest(1, 0);
183+
greatest
184+
----------
185+
1
186+
(1 row)
187+
172188
-- dump out the tables
173189
SELECT * FROM my_vertices;
174190
u

regress/sql/cypher.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ CREATE TABLE my_edges AS
9494
CREATE TABLE my_detailed_paths AS
9595
(SELECT * FROM cypher('issue_1767', $$ MATCH p=(u)-[e]->(v) RETURN u,e,v,p $$) as (u agtype, e agtype, v agtype, p agtype));
9696

97+
--
98+
-- Issue 2256: A segmentation fault occurs when calling the coalesce function
99+
-- This also occurs with the greatest function too.
100+
--
101+
SELECT * FROM coalesce(1, 0);
102+
SELECT * FROM greatest(1, 0);
103+
97104
-- dump out the tables
98105
SELECT * FROM my_vertices;
99106
SELECT * FROM my_edges;

src/backend/parser/cypher_analyze.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,20 @@ static bool convert_cypher_walker(Node *node, ParseState *pstate)
171171
* JsonConstructorExpr - wrapper over FuncExpr/Aggref/WindowFunc for
172172
* SQL/JSON constructors
173173
*
174-
* These are a special case that needs to be ignored.
174+
* Added the following, although only the first 2 caused crashes in tests -
175+
* CoalesceExpr, MinMaxExpr, CaseExpr, XmlExpr, ArrayExpr, RowExpr
176+
*
177+
* These are all special case that needs to be ignored.
175178
*
176179
*/
177180
if (IsA(funcexpr, SQLValueFunction)
178-
|| IsA(funcexpr, CoerceViaIO)
179-
|| IsA(funcexpr, Var) || IsA(funcexpr, OpExpr)
180-
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr)
181-
|| IsA(funcexpr, JsonConstructorExpr))
181+
|| IsA(funcexpr, CoerceViaIO)
182+
|| IsA(funcexpr, Var) || IsA(funcexpr, OpExpr)
183+
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr)
184+
|| IsA(funcexpr, JsonConstructorExpr)
185+
|| IsA(funcexpr, CoalesceExpr) || IsA(funcexpr, MinMaxExpr)
186+
|| IsA(funcexpr, CaseExpr) || IsA(funcexpr, XmlExpr)
187+
|| IsA(funcexpr, ArrayExpr) || IsA(funcexpr, RowExpr))
182188
{
183189
return false;
184190
}
@@ -346,14 +352,20 @@ static bool is_func_cypher(FuncExpr *funcexpr)
346352
* JsonConstructorExpr - wrapper over FuncExpr/Aggref/WindowFunc for
347353
* SQL/JSON constructors
348354
*
349-
* These are a special case that needs to be ignored.
355+
* Added the following, although only the first 2 caused crashes in tests -
356+
* CoalesceExpr, MinMaxExpr, CaseExpr, XmlExpr, ArrayExpr, RowExpr
357+
*
358+
* These are all special case that needs to be ignored.
350359
*
351360
*/
352361
if (IsA(funcexpr, SQLValueFunction)
353362
|| IsA(funcexpr, CoerceViaIO)
354363
|| IsA(funcexpr, Var) || IsA(funcexpr, OpExpr)
355364
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr)
356-
|| IsA(funcexpr, JsonConstructorExpr))
365+
|| IsA(funcexpr, JsonConstructorExpr)
366+
|| IsA(funcexpr, CoalesceExpr) || IsA(funcexpr, MinMaxExpr)
367+
|| IsA(funcexpr, CaseExpr) || IsA(funcexpr, XmlExpr)
368+
|| IsA(funcexpr, ArrayExpr) || IsA(funcexpr, RowExpr))
357369
{
358370
return false;
359371
}

0 commit comments

Comments
 (0)