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

Skip to content

Commit e6a4801

Browse files
committed
In transformRowExpr(), check for too many columns in the row.
A RowExpr with more than MaxTupleAttributeNumber columns would fail at execution anyway, since we cannot form a tuple datum with more than that many columns. While heap_form_tuple() has a check for too many columns, it emerges that there are some intermediate bits of code that don't check and can be driven to failure with sufficiently many columns. Checking this at parse time seems like the most appropriate place to install a defense, since we already check SELECT list length there. While at it, make the SELECT-list-length error use the same errcode (TOO_MANY_COLUMNS) as heap_form_tuple does, rather than the generic PROGRAM_LIMIT_EXCEEDED. Per bug #17561 from Egor Chindyaskin. The given test case crashes in all supported branches (and probably a lot further back), so patch all. Discussion: https://postgr.es/m/[email protected]
1 parent 6ffaf75 commit e6a4801

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/backend/parser/parse_expr.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "postgres.h"
1717

18+
#include "access/htup_details.h"
1819
#include "catalog/pg_type.h"
1920
#include "commands/dbcommands.h"
2021
#include "miscadmin.h"
@@ -2176,6 +2177,14 @@ transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
21762177
newr->args = transformExpressionList(pstate, r->args,
21772178
pstate->p_expr_kind, allowDefault);
21782179

2180+
/* Disallow more columns than will fit in a tuple */
2181+
if (list_length(newr->args) > MaxTupleAttributeNumber)
2182+
ereport(ERROR,
2183+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
2184+
errmsg("ROW expressions can have at most %d entries",
2185+
MaxTupleAttributeNumber),
2186+
parser_errposition(pstate, r->location)));
2187+
21792188
/* Barring later casting, we consider the type RECORD */
21802189
newr->row_typeid = RECORDOID;
21812190
newr->row_format = COERCE_IMPLICIT_CAST;

src/backend/parser/parse_node.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ free_parsestate(ParseState *pstate)
8383
*/
8484
if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
8585
ereport(ERROR,
86-
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
86+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
8787
errmsg("target lists can have at most %d entries",
8888
MaxTupleAttributeNumber)));
8989

0 commit comments

Comments
 (0)