]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
In transformRowExpr(), check for too many columns in the row.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 29 Jul 2022 17:30:50 +0000 (13:30 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 29 Jul 2022 17:30:50 +0000 (13:30 -0400)
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/17561-80350151b9ad2ad4@postgresql.org

src/backend/parser/parse_expr.c
src/backend/parser/parse_node.c

index cd2338d659b6e4483f4ea77ae446a1e8e9240461..f963040b4a521ad45b9f4abf81b94f1647d2ebcd 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "postgres.h"
 
+#include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "commands/dbcommands.h"
 #include "miscadmin.h"
@@ -2185,6 +2186,14 @@ transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
        newr->args = transformExpressionList(pstate, r->args,
                                                                                 pstate->p_expr_kind, allowDefault);
 
+       /* Disallow more columns than will fit in a tuple */
+       if (list_length(newr->args) > MaxTupleAttributeNumber)
+               ereport(ERROR,
+                               (errcode(ERRCODE_TOO_MANY_COLUMNS),
+                                errmsg("ROW expressions can have at most %d entries",
+                                               MaxTupleAttributeNumber),
+                                parser_errposition(pstate, r->location)));
+
        /* Barring later casting, we consider the type RECORD */
        newr->row_typeid = RECORDOID;
        newr->row_format = COERCE_IMPLICIT_CAST;
index d2672882d76cee21acbd9f7c45f7d6af7f09260f..596a9c4fb4a1a35e9b6d345b9fa045883c456c68 100644 (file)
@@ -83,7 +83,7 @@ free_parsestate(ParseState *pstate)
         */
        if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
                ereport(ERROR,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                               (errcode(ERRCODE_TOO_MANY_COLUMNS),
                                 errmsg("target lists can have at most %d entries",
                                                MaxTupleAttributeNumber)));