Commit
90189eefc1e1 narrowed pg_attribute.attinhcount and
pg_constraint.coninhcount from 32 to 16 bits, but kept other related
structs with 32-bit wide fields: ColumnDef and CookedConstraint contain
an int 'inhcount' field which is itself checked for overflow on
increments, but there's no check that the values aren't above INT16_MAX
before assigning to the catalog columns. This means that a creative
user can get a inconsistent table definition and override some
protections.
Fix it by changing those other structs to also use int16.
Also, modernize style by using pg_add_s16_overflow for overflow testing
instead of checking for negative values.
We also have Constraint.inhcount, which is here removed completely.
This was added by commit
b0e96f311985 and not removed by its revert at
6f8bb7c1e961. It is not needed by the upcoming not-null constraints
patch.
This is mostly academic, so we agreed not to backpatch to avoid ABI
problems.
Bump catversion because of the changes to parse nodes.
Co-authored-by: Álvaro Herrera <[email protected]>
Co-authored-by: 何建 (jian he) <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/
202410081611[email protected]
Oid new_array_type);
static void RelationRemoveInheritance(Oid relid);
static Oid StoreRelCheck(Relation rel, const char *ccname, Node *expr,
- bool is_validated, bool is_local, int inhcount,
+ bool is_validated, bool is_local, int16 inhcount,
bool is_no_inherit, bool is_internal);
static void StoreConstraints(Relation rel, List *cooked_constraints,
bool is_internal);
*/
static Oid
StoreRelCheck(Relation rel, const char *ccname, Node *expr,
- bool is_validated, bool is_local, int inhcount,
+ bool is_validated, bool is_local, int16 inhcount,
bool is_no_inherit, bool is_internal)
{
char *ccbin;
{
if (is_local)
con->conislocal = true;
- else
- con->coninhcount++;
-
- if (con->coninhcount < 0)
+ else if (pg_add_s16_overflow(con->coninhcount, 1,
+ &con->coninhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
bool islocal;
bool noinherit;
bool is_without_overlaps;
- int inhcount;
+ int16 inhcount;
deferrable = (constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) != 0;
initdeferred = (constr_flags & INDEX_CONSTR_CREATE_INIT_DEFERRED) != 0;
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
Node *conExpr,
const char *conBin,
bool conIsLocal,
- int conInhCount,
+ int16 conInhCount,
bool conNoInherit,
bool conPeriod,
bool is_internal)
childConstrId);
constrForm->conislocal = false;
- constrForm->coninhcount++;
- if (constrForm->coninhcount < 0)
+ if (pg_add_s16_overflow(constrForm->coninhcount, 1,
+ &constrForm->coninhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
+
constrForm->conparentid = parentConstrId;
CatalogTupleUpdate(constrRel, &tuple->t_self, newtup);
#include "commands/typecmds.h"
#include "commands/user.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
if (equal(expr, ccon->expr))
{
/* OK to merge constraint with existing */
- ccon->inhcount++;
- if (ccon->inhcount < 0)
+ if (pg_add_s16_overflow(ccon->inhcount, 1,
+ &ccon->inhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
* Default and other constraints are handled by the caller.
*/
- prevdef->inhcount++;
- if (prevdef->inhcount < 0)
+ if (pg_add_s16_overflow(prevdef->inhcount, 1,
+ &prevdef->inhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
get_collation_name(childatt->attcollation))));
/* Bump the existing child att's inhcount */
- childatt->attinhcount++;
- if (childatt->attinhcount < 0)
+ if (pg_add_s16_overflow(childatt->attinhcount, 1,
+ &childatt->attinhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
Oid constrOid;
char *conname;
bool conislocal;
- int coninhcount;
+ int16 coninhcount;
bool connoinherit;
Oid deleteTriggerOid,
updateTriggerOid;
NULL,
NULL,
NULL,
- false,
- 1,
- false,
+ false, /* conIsLocal */
+ 1, /* conInhCount */
+ false, /* conNoInherit */
with_period, /* conPeriod */
false);
NULL,
NULL,
NULL,
- false, /* islocal */
- 1, /* inhcount */
+ false, /* conIsLocal */
+ 1, /* conInhCount */
false, /* conNoInherit */
with_period, /* conPeriod */
true);
* OK, bump the child column's inheritance count. (If we fail
* later on, this change will just roll back.)
*/
- child_att->attinhcount++;
- if (child_att->attinhcount < 0)
+ if (pg_add_s16_overflow(child_att->attinhcount, 1,
+ &child_att->attinhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
*/
child_copy = heap_copytuple(child_tuple);
child_con = (Form_pg_constraint) GETSTRUCT(child_copy);
- child_con->coninhcount++;
- if (child_con->coninhcount < 0)
+
+ if (pg_add_s16_overflow(child_con->coninhcount, 1,
+ &child_con->coninhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 202410111
+#define CATALOG_VERSION_NO 202410112
#endif
Node *expr; /* transformed default or check expr */
bool skip_validation; /* skip validation? (only for CHECK) */
bool is_local; /* constraint has local (non-inherited) def */
- int inhcount; /* number of times constraint is inherited */
+ int16 inhcount; /* number of times constraint is inherited */
bool is_no_inherit; /* constraint has local def and cannot be
* inherited */
} CookedConstraint;
Node *conExpr,
const char *conBin,
bool conIsLocal,
- int conInhCount,
+ int16 conInhCount,
bool conNoInherit,
bool conPeriod,
bool is_internal);
char *colname; /* name of column */
TypeName *typeName; /* type of column */
char *compression; /* compression method for column */
- int inhcount; /* number of times column is inherited */
+ int16 inhcount; /* number of times column is inherited */
bool is_local; /* column has local (non-inherited) def'n */
bool is_not_null; /* NOT NULL constraint specified? */
bool is_from_type; /* column definition came from table type */
char *cooked_expr; /* CHECK or DEFAULT expression, as
* nodeToString representation */
char generated_when; /* ALWAYS or BY DEFAULT */
- int inhcount; /* initial inheritance count to apply, for
- * "raw" NOT NULL constraints */
bool nulls_not_distinct; /* null treatment for UNIQUE constraints */
List *keys; /* String nodes naming referenced key
* column(s); for UNIQUE/PK/NOT NULL */