Limit max parameter number with MaxAllocSize
authorPeter Eisentraut <peter@eisentraut.org>
Tue, 2 Jul 2024 07:24:04 +0000 (09:24 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Tue, 2 Jul 2024 07:29:26 +0000 (09:29 +0200)
MaxAllocSize puts an upper bound on the largest possible parameter
number ($268435455).  Use that limit instead of INT_MAX to report that
no parameters exist beyond that point instead of reporting an error
about the maximum allocation size being exceeded.

Author: Erik Wienhold <ewie@ewie.name>
Discussion: https://www.postgresql.org/message-id/flat/5d216d1c-91f6-4cbe-95e2-b4cbd930520c@ewie.name

src/backend/parser/parse_param.c
src/test/regress/expected/prepare.out
src/test/regress/sql/prepare.sql

index dbf1a7dff089b9f784355341767364e8988805ed..b617591ef682d75fe72851a87b88e3e6c3ca7cad 100644 (file)
@@ -31,6 +31,7 @@
 #include "parser/parse_param.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
+#include "utils/memutils.h"
 
 
 typedef struct FixedParamState
@@ -136,7 +137,7 @@ variable_paramref_hook(ParseState *pstate, ParamRef *pref)
        Param      *param;
 
        /* Check parameter number is in range */
-       if (paramno <= 0 || paramno > INT_MAX / sizeof(Oid))
+       if (paramno <= 0 || paramno > MaxAllocSize / sizeof(Oid))
                ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_PARAMETER),
                                 errmsg("there is no parameter $%d", paramno),
index 5815e17b39cc8c933b2c4ba9d00ecfbb96206bcd..853cbed248eca783a9778b4a3ae894e9e367b36a 100644 (file)
@@ -184,6 +184,11 @@ SELECT name, statement, parameter_types, result_types FROM pg_prepared_statement
       |     UPDATE tenk1 SET stringu1 = $2 WHERE unique1 = $1;           |                                                    | 
 (6 rows)
 
+-- max parameter number and one above
+PREPARE q9 AS SELECT $268435455, $268435456;
+ERROR:  there is no parameter $268435456
+LINE 1: PREPARE q9 AS SELECT $268435455, $268435456;
+                                         ^
 -- test DEALLOCATE ALL;
 DEALLOCATE ALL;
 SELECT name, statement, parameter_types FROM pg_prepared_statements
index c6098dc95ceee7cf2be3d8f18cd1bc7a519eec7f..1536f802d5eb9c30deb31ffd384a5fd3fb33a477 100644 (file)
@@ -78,6 +78,9 @@ PREPARE q8 AS
 SELECT name, statement, parameter_types, result_types FROM pg_prepared_statements
     ORDER BY name;
 
+-- max parameter number and one above
+PREPARE q9 AS SELECT $268435455, $268435456;
+
 -- test DEALLOCATE ALL;
 DEALLOCATE ALL;
 SELECT name, statement, parameter_types FROM pg_prepared_statements