Add missing check_stack_depth() to some recursive functions
authorAlexander Korotkov <akorotkov@postgresql.org>
Fri, 16 Feb 2024 14:02:00 +0000 (16:02 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Fri, 16 Feb 2024 14:02:00 +0000 (16:02 +0200)
Reported-by: Egor Chindyaskin, Alexander Lakhin
Discussion: https://postgr.es/m/1672760457.940462079%40f306.i.mail.ru

src/backend/catalog/dependency.c
src/backend/catalog/heap.c
src/backend/commands/tablecmds.c
src/backend/optimizer/util/clauses.c
src/backend/utils/adt/jsonpath_exec.c

index e742c78ea35692da0c0cc634cfc23cec4009e30f..df9886efc95e7056e81e31412dd9cf1967ceba6c 100644 (file)
@@ -76,6 +76,7 @@
 #include "commands/trigger.h"
 #include "commands/typecmds.h"
 #include "funcapi.h"
+#include "miscadmin.h"
 #include "nodes/nodeFuncs.h"
 #include "parser/parsetree.h"
 #include "rewrite/rewriteRemove.h"
@@ -524,6 +525,12 @@ findDependentObjects(const ObjectAddress *object,
    if (stack_address_present_add_flags(object, objflags, stack))
        return;
 
+   /*
+    * since this function recurses, it could be driven to stack overflow,
+    * because of the deep dependency tree, not only due to dependency loops.
+    */
+   check_stack_depth();
+
    /*
     * It's also possible that the target object has already been completely
     * processed and put into targetObjects.  If so, again we just add the
index c73f7bcd0113fe675b0409329732883b8144fe55..252e106cadacdef49e2717ba227f8f37016ad614 100644 (file)
@@ -552,6 +552,9 @@ CheckAttributeType(const char *attname,
    char        att_typtype = get_typtype(atttypid);
    Oid         att_typelem;
 
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    if (att_typtype == TYPTYPE_PSEUDO)
    {
        /*
index 237eeeec67e0dfd6282d1b4e9bcef5ac504b3d6e..46ece07338a31d57b8656667b2635f6a59119a3f 100644 (file)
@@ -7035,6 +7035,9 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
    ObjectAddress address;
    TupleDesc   tupdesc;
 
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    /* At top level, permission check was done in ATPrepCmd, else do it */
    if (recursing)
        ATSimplePermissions((*cmd)->subtype, rel, ATT_TABLE | ATT_FOREIGN_TABLE);
@@ -9083,6 +9086,10 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
 
    /* Initialize addrs on the first invocation */
    Assert(!recursing || addrs != NULL);
+
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    if (!recursing)
        addrs = new_object_addresses();
 
@@ -11636,6 +11643,9 @@ ATExecAlterConstrRecurse(Constraint *cmdcon, Relation conrel, Relation tgrel,
    Oid         refrelid;
    bool        changed = false;
 
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    currcon = (Form_pg_constraint) GETSTRUCT(contuple);
    conoid = currcon->oid;
    refrelid = currcon->confrelid;
@@ -12716,6 +12726,9 @@ dropconstraint_internal(Relation rel, HeapTuple constraintTup, DropBehavior beha
    /* Guard against stack overflow due to overly deep inheritance tree. */
    check_stack_depth();
 
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    /* At top level, permission check was done in ATPrepCmd, else do it */
    if (recursing)
        ATSimplePermissions(AT_DropConstraint, rel, ATT_TABLE | ATT_FOREIGN_TABLE);
index 94eb56a1e79b075a59a424874a700e253db72c7c..edc25d712e94ef4c18eebd89f3f55bcc6f30c9b0 100644 (file)
@@ -2423,6 +2423,10 @@ static Node *
 eval_const_expressions_mutator(Node *node,
                               eval_const_expressions_context *context)
 {
+
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    if (node == NULL)
        return NULL;
    switch (nodeTag(node))
index 8372863de74e7da257a1f7aea509f4cf89342c5f..50fa724b728f9b6ba135b50afc768bb560ff9a7c 100644 (file)
@@ -1674,6 +1674,9 @@ executeBoolItem(JsonPathExecContext *cxt, JsonPathItem *jsp,
    JsonPathBool res;
    JsonPathBool res2;
 
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    if (!canHaveNext && jspHasNext(jsp))
        elog(ERROR, "boolean jsonpath item cannot have next item");