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

Skip to content

Commit 83f1793

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 8b1ec7d commit 83f1793

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/backend/parser/parse_relation.c

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

18421842
/*
18431843
* Use the column definition list to construct a tupdesc and fill
1844-
* in the RangeTblFunction's lists.
1844+
* in the RangeTblFunction's lists. Limit number of columns to
1845+
* MaxHeapAttributeNumber, because CheckAttributeNamesTypes will.
18451846
*/
1847+
if (list_length(coldeflist) > MaxHeapAttributeNumber)
1848+
ereport(ERROR,
1849+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
1850+
errmsg("column definition lists can have at most %d entries",
1851+
MaxHeapAttributeNumber),
1852+
parser_errposition(pstate,
1853+
exprLocation((Node *) coldeflist))));
18461854
tupdesc = CreateTemplateTupleDesc(list_length(coldeflist));
18471855
i = 1;
18481856
foreach(col, coldeflist)
@@ -1922,6 +1930,15 @@ addRangeTableEntryForFunction(ParseState *pstate,
19221930
if (rangefunc->ordinality)
19231931
totalatts++;
19241932

1933+
/* Disallow more columns than will fit in a tuple */
1934+
if (totalatts > MaxTupleAttributeNumber)
1935+
ereport(ERROR,
1936+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
1937+
errmsg("functions in FROM can return at most %d columns",
1938+
MaxTupleAttributeNumber),
1939+
parser_errposition(pstate,
1940+
exprLocation((Node *) funcexprs))));
1941+
19251942
/* Merge the tuple descs of each function into a composite one */
19261943
tupdesc = CreateTemplateTupleDesc(totalatts);
19271944
natts = 0;
@@ -2004,11 +2021,23 @@ addRangeTableEntryForTableFunc(ParseState *pstate,
20042021
Alias *eref;
20052022
int numaliases;
20062023

2024+
Assert(pstate != NULL);
2025+
2026+
/* Disallow more columns than will fit in a tuple */
2027+
if (list_length(tf->colnames) > MaxTupleAttributeNumber)
2028+
ereport(ERROR,
2029+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
2030+
errmsg("functions in FROM can return at most %d columns",
2031+
MaxTupleAttributeNumber),
2032+
parser_errposition(pstate,
2033+
exprLocation((Node *) tf))));
2034+
Assert(list_length(tf->coltypes) == list_length(tf->colnames));
2035+
Assert(list_length(tf->coltypmods) == list_length(tf->colnames));
2036+
Assert(list_length(tf->colcollations) == list_length(tf->colnames));
2037+
20072038
refname = alias ? alias->aliasname :
20082039
pstrdup(tf->functype == TFT_XMLTABLE ? "xmltable" : "json_table");
20092040

2010-
Assert(pstate != NULL);
2011-
20122041
rte->rtekind = RTE_TABLEFUNC;
20132042
rte->relid = InvalidOid;
20142043
rte->subquery = NULL;

0 commit comments

Comments
 (0)