Since b8d7f053c5c2bf2a7e8734fe3327f6a8bc711755 (Andres in Cc), if you
write queries which result in infinite recursion (or just too many
nested function calls), execution ends with segfault instead of intended
exhausted max_stack_depth:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000df851e in DirectFunctionCall1Coll (
    func=<error reading variable: Cannot access memory at address
    collation=<error reading variable: Cannot access memory at address
    arg1=<error reading variable: Cannot access memory at address
0x7ffef5972f88>) at fmgr.c:708

Please find attached a trivial patch to fix this.  I'm not sure
ExecMakeTableFunctionResult() is the best or only place that needs to
check the stack depth.

I also attached a simple sql file to reproduce the issue if needed.
Should I add a regression test based on it?

Julien Rouhaud
http://dalibo.com - http://dalibo.org

Attachment: segfault.sql
Description: application/sql

diff --git a/src/backend/executor/execSRF.c b/src/backend/executor/execSRF.c
index 138e86ac67..9d257d4d88 100644
--- a/src/backend/executor/execSRF.c
+++ b/src/backend/executor/execSRF.c
@@ -117,6 +117,9 @@ ExecMakeTableFunctionResult(SetExprState *setexpr,
 	MemoryContext oldcontext;
 	bool		first_time = true;
+	/* Guard against stack overflow due to overly nested functions call */
+	check_stack_depth();
 	callerContext = CurrentMemoryContext;
 	funcrettype = exprType((Node *) setexpr->expr);
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:

Reply via email to