Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dWCWr-0004Zb-Hw for pgsql-hackers@arkaria.postgresql.org; Sat, 15 Jul 2017 02:09:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dWCWp-000273-TP for pgsql-hackers@arkaria.postgresql.org; Sat, 15 Jul 2017 02:09:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dWCVI-0007qk-Rb for pgsql-hackers@postgresql.org; Sat, 15 Jul 2017 02:08:20 +0000 Received: from mimolette.dalibo.net ([212.85.157.144] helo=mail.dalibo.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dWCVE-0000t8-5B for pgsql-hackers@postgresql.org; Sat, 15 Jul 2017 02:08:19 +0000 Received: from [192.168.1.3] (unknown [118.179.234.203]) by mail.dalibo.com (Postfix) with ESMTPSA id C0A962C0188; Sat, 15 Jul 2017 04:08:13 +0200 (CEST) From: Julien Rouhaud Subject: segfault in HEAD when too many nested functions call To: pgsql-hackers Cc: Andres Freund Message-ID: Date: Sat, 15 Jul 2017 04:07:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------81E5B0FE17C48DA464012C4E" Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------81E5B0FE17C48DA464012C4E Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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=, collation=, arg1=) 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 --------------81E5B0FE17C48DA464012C4E Content-Type: application/sql; name="segfault.sql" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="segfault.sql" Q1JFQVRFIFRBQkxFIHB1YmxpYy50IChpZCBpbnRlZ2VyKTsKCkNSRUFURSBP UiBSRVBMQUNFIEZVTkNUSU9OIHB1YmxpYy5mKCkKIFJFVFVSTlMgU0VUT0Yg dAogTEFOR1VBR0UgcGxwZ3NxbAogU0VDVVJJVFkgREVGSU5FUgpBUyAkJApE RUNMQVJFCiByZWMgUkVDT1JEOwpCRUdJTgogICAgRk9SIHJlYyBJTiBTRUxF Q1QgKiBGUk9NIHQKICAgIExPT1AKICAgICAgICBSRVRVUk4gTkVYVCByZWM7 CiAgICBFTkQgTE9PUDsKICAgIFJFVFVSTjsKRU5EOwokJDsKCkNSRUFURSBT Q0hFTUEgczsKQ1JFQVRFIFZJRVcgcy50IEFTIFNFTEVDVCAqIEZST00gcHVi bGljLmYoKTsKU0VUIHNlYXJjaF9wYXRoIFRPIHM7ClNFTEVDVCAqIGZyb20g dDsK --------------81E5B0FE17C48DA464012C4E Content-Type: text/x-patch; name="fix_segfault_recursion.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="fix_segfault_recursion.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 =3D true; =20 + /* Guard against stack overflow due to overly nested functions call */ + check_stack_depth(); + callerContext =3D CurrentMemoryContext; =20 funcrettype =3D exprType((Node *) setexpr->expr); --------------81E5B0FE17C48DA464012C4E Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------81E5B0FE17C48DA464012C4E--