Skip to content

Commit adc3ae6

Browse files
committed
Check maximum number of columns in function RTEs, too.
I thought commit fd96d14 had plugged all the holes of this sort, but no, function RTEs could produce oversize tuples too, either via long coldeflists or just from multiple functions in one RTE. (I'm pretty sure the other variants of base RTEs aren't a problem, because they ultimately refer to either a table or a sub-SELECT, whose widths are enforced elsewhere. But we explicitly allow join RTEs to be overwidth, as long as you don't try to form their tuple result.) Per further discussion of bug #17561. As before, patch all branches. Discussion: https://postgr.es/m/[email protected]
1 parent 07abcd9 commit adc3ae6

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

Diff for: src/backend/parser/parse_relation.c

+32-3
Original file line numberDiff line numberDiff line change
@@ -1830,8 +1830,16 @@ addRangeTableEntryForFunction(ParseState *pstate,
18301830

18311831
/*
18321832
* Use the column definition list to construct a tupdesc and fill
1833-
* in the RangeTblFunction's lists.
1833+
* in the RangeTblFunction's lists. Limit number of columns to
1834+
* MaxHeapAttributeNumber, because CheckAttributeNamesTypes will.
18341835
*/
1836+
if (list_length(coldeflist) > MaxHeapAttributeNumber)
1837+
ereport(ERROR,
1838+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
1839+
errmsg("column definition lists can have at most %d entries",
1840+
MaxHeapAttributeNumber),
1841+
parser_errposition(pstate,
1842+
exprLocation((Node *) coldeflist))));
18351843
tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
18361844
i = 1;
18371845
foreach(col, coldeflist)
@@ -1911,6 +1919,15 @@ addRangeTableEntryForFunction(ParseState *pstate,
19111919
if (rangefunc->ordinality)
19121920
totalatts++;
19131921

1922+
/* Disallow more columns than will fit in a tuple */
1923+
if (totalatts > MaxTupleAttributeNumber)
1924+
ereport(ERROR,
1925+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
1926+
errmsg("functions in FROM can return at most %d columns",
1927+
MaxTupleAttributeNumber),
1928+
parser_errposition(pstate,
1929+
exprLocation((Node *) funcexprs))));
1930+
19141931
/* Merge the tuple descs of each function into a composite one */
19151932
tupdesc = CreateTemplateTupleDesc(totalatts);
19161933
natts = 0;
@@ -1993,11 +2010,23 @@ addRangeTableEntryForTableFunc(ParseState *pstate,
19932010
Alias *eref;
19942011
int numaliases;
19952012

2013+
Assert(pstate != NULL);
2014+
2015+
/* Disallow more columns than will fit in a tuple */
2016+
if (list_length(tf->colnames) > MaxTupleAttributeNumber)
2017+
ereport(ERROR,
2018+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
2019+
errmsg("functions in FROM can return at most %d columns",
2020+
MaxTupleAttributeNumber),
2021+
parser_errposition(pstate,
2022+
exprLocation((Node *) tf))));
2023+
Assert(list_length(tf->coltypes) == list_length(tf->colnames));
2024+
Assert(list_length(tf->coltypmods) == list_length(tf->colnames));
2025+
Assert(list_length(tf->colcollations) == list_length(tf->colnames));
2026+
19962027
refname = alias ? alias->aliasname :
19972028
pstrdup(tf->functype == TFT_XMLTABLE ? "xmltable" : "json_table");
19982029

1999-
Assert(pstate != NULL);
2000-
20012030
rte->rtekind = RTE_TABLEFUNC;
20022031
rte->relid = InvalidOid;
20032032
rte->subquery = NULL;

0 commit comments

Comments
 (0)