pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Julien Rouhaud <julien.rouhaud@dalibo.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Cc: Andres Freund <andres@anarazel.de>
Subject: segfault in HEAD when too many nested functions call
Date: Sat, 15 Jul 2017 04:07:03 +0200
Message-ID: <b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

Hello,

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
0x7ffef5972f98>,
    collation=<error reading variable: Cannot access memory at address
0x7ffef5972f94>,
    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

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

  [application/sql] segfault.sql (363B, ../b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com/2-segfault.sql)
  download

  [text/x-patch] fix_segfault_recursion.diff (504B, ../b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com/3-fix_segfault_recursion.diff)
  download | inline diff:
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);

view thread (42+ messages)  latest in thread

Message-ID: <b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com>
Permalink:  ../b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com/
Also on:    postgresql.org/message-id/b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: julien.rouhaud@dalibo.com, andres@anarazel.de
  Subject: Re: segfault in HEAD when too many nested functions call
  In-Reply-To: <b0af9eaa-130c-60d0-9e4e-7a135b1e0c76@dalibo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox