From 62999b93251eefbfcf900a0bb202721b0a422ffd Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 10 Dec 2018 11:12:43 -0500 Subject: [PATCH] Add stack depth checks to key recursive functions in backend/nodes/*.c. Although copyfuncs.c has a check_stack_depth call in its recursion, equalfuncs.c, outfuncs.c, and readfuncs.c lacked one. This seems unwise. Likewise fix planstate_tree_walker(), in branches where that exists. Discussion: https://postgr.es/m/30253.1544286631@sss.pgh.pa.us --- src/backend/nodes/equalfuncs.c | 4 ++++ src/backend/nodes/nodeFuncs.c | 3 +++ src/backend/nodes/outfuncs.c | 4 ++++ src/backend/nodes/readfuncs.c | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index 2435b26f52..88ed816808 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -29,6 +29,7 @@ #include "postgres.h" +#include "miscadmin.h" #include "nodes/extensible.h" #include "nodes/relation.h" #include "utils/datum.h" @@ -3001,6 +3002,9 @@ equal(const void *a, const void *b) if (nodeTag(a) != nodeTag(b)) return false; + /* Guard against stack overflow due to overly complex expressions */ + check_stack_depth(); + switch (nodeTag(a)) { /* diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index a10014f755..54b3dcff3e 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -3726,6 +3726,9 @@ planstate_tree_walker(PlanState *planstate, Plan *plan = planstate->plan; ListCell *lc; + /* Guard against stack overflow due to overly complex plan trees */ + check_stack_depth(); + /* initPlan-s */ if (planstate_walk_subplans(planstate->initPlan, walker, context)) return true; diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 6c3dad9ab3..96c6123545 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -30,6 +30,7 @@ #include #include "lib/stringinfo.h" +#include "miscadmin.h" #include "nodes/extensible.h" #include "nodes/plannodes.h" #include "nodes/relation.h" @@ -3703,6 +3704,9 @@ _outPartitionRangeDatum(StringInfo str, const PartitionRangeDatum *node) void outNode(StringInfo str, const void *obj) { + /* Guard against stack overflow due to overly complex expressions */ + check_stack_depth(); + if (obj == NULL) appendStringInfoString(str, "<>"); else if (IsA(obj, List) ||IsA(obj, IntList) || IsA(obj, OidList)) diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 4362b00a42..2ceb1e2ac2 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -29,6 +29,7 @@ #include #include "fmgr.h" +#include "miscadmin.h" #include "nodes/extensible.h" #include "nodes/parsenodes.h" #include "nodes/plannodes.h" @@ -2539,6 +2540,9 @@ parseNodeString(void) READ_TEMP_LOCALS(); + /* Guard against stack overflow due to overly complex expressions */ + check_stack_depth(); + token = pg_strtok(&length); #define MATCH(tokname, namelen) \ -- 2.40.0