Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0f7dd9

Browse files
committed
Check stack depth in new recursive functions
Commit b0e96f3 introduced a bunch of recursive functions, but failed to make them check for stack depth. This can cause the backend to crash when operating on inheritance hierarchies several thousands deep. Protect the code by adding the missing stack depth checks. Reported-by: Alexander Lakhin <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 76db9cb commit b0f7dd9

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/backend/commands/tablecmds.c

+12
Original file line numberDiff line numberDiff line change
@@ -7630,6 +7630,9 @@ set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, bool recurse,
76307630
Form_pg_attribute attForm;
76317631
bool retval = false;
76327632

7633+
/* Guard against stack overflow due to overly deep inheritance tree. */
7634+
check_stack_depth();
7635+
76337636
tuple = SearchSysCacheCopyAttNum(RelationGetRelid(rel), attnum);
76347637
if (!HeapTupleIsValid(tuple))
76357638
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
@@ -7716,6 +7719,9 @@ ATExecSetNotNull(List **wqueue, Relation rel, char *conName, char *colName,
77167719
bool is_no_inherit = false;
77177720
List *ready = NIL;
77187721

7722+
/* Guard against stack overflow due to overly deep inheritance tree. */
7723+
check_stack_depth();
7724+
77197725
/*
77207726
* In cases of multiple inheritance, we might visit the same child more
77217727
* than once. In the topmost call, set up a list that we fill with all
@@ -9359,6 +9365,9 @@ ATAddCheckNNConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
93599365
ListCell *child;
93609366
ObjectAddress address = InvalidObjectAddress;
93619367

9368+
/* Guard against stack overflow due to overly deep inheritance tree. */
9369+
check_stack_depth();
9370+
93629371
/* At top level, permission check was done in ATPrepCmd, else do it */
93639372
if (recursing)
93649373
ATSimplePermissions(AT_AddConstraint, rel, ATT_TABLE | ATT_FOREIGN_TABLE);
@@ -12428,6 +12437,9 @@ dropconstraint_internal(Relation rel, HeapTuple constraintTup, DropBehavior beha
1242812437
return InvalidObjectAddress;
1242912438
*readyRels = lappend_oid(*readyRels, RelationGetRelid(rel));
1243012439

12440+
/* Guard against stack overflow due to overly deep inheritance tree. */
12441+
check_stack_depth();
12442+
1243112443
/* At top level, permission check was done in ATPrepCmd, else do it */
1243212444
if (recursing)
1243312445
ATSimplePermissions(AT_DropConstraint, rel, ATT_TABLE | ATT_FOREIGN_TABLE);

0 commit comments

Comments
 (0)