1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

Fix mvdistinct and dependencies size calculations

The formulas used to calculate size while (de)serializing mvndistinct
and functional dependencies were based on offset() of the structs. But
that is incorrect, because the structures are not copied directly, we
we copy the individual fields directly.

At the moment this works fine, because there is no alignment padding
on any platform we support. But it might break if we ever added some
fields into any of the structs, for example. It's also confusing.

Fixed by reworking the macros to directly sum sizes of serialized
fields. The macros are now useful only for serialiation, so there is
no point in keeping them in the public header file. So make them
private by moving them to the .c files.

Also adds a couple more asserts to check the serialization, and fixes
an incorrect allocation of MVDependency instead of (MVDependency *).

Reported-By: Tom Lane
Discussion: https://postgr.es/m/29785.1555365602@sss.pgh.pa.us
This commit is contained in:
Tomas Vondra
2019-04-21 19:54:15 +02:00
parent bfbc150e39
commit d08c44f7a4
3 changed files with 56 additions and 40 deletions

View File

@@ -29,10 +29,6 @@ typedef struct MVNDistinctItem
Bitmapset *attrs; /* attr numbers of items */
} MVNDistinctItem;
/* size of the struct, excluding attribute list */
#define SizeOfMVNDistinctItem \
(offsetof(MVNDistinctItem, ndistinct) + sizeof(double))
/* A MVNDistinct object, comprising all possible combinations of columns */
typedef struct MVNDistinct
{
@@ -42,13 +38,7 @@ typedef struct MVNDistinct
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER];
} MVNDistinct;
/* size of the struct excluding the items array */
#define SizeOfMVNDistinct (offsetof(MVNDistinct, nitems) + sizeof(uint32))
/* size of the struct excluding the items array */
#define SizeOfMVNDistinct (offsetof(MVNDistinct, nitems) + sizeof(uint32))
/* Multivariate functional dependencies */
#define STATS_DEPS_MAGIC 0xB4549A2C /* marks serialized bytea */
#define STATS_DEPS_TYPE_BASIC 1 /* basic dependencies type */
@@ -63,10 +53,6 @@ typedef struct MVDependency
AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]; /* attribute numbers */
} MVDependency;
/* size of the struct excluding the deps array */
#define SizeOfDependency \
(offsetof(MVDependency, nattributes) + sizeof(AttrNumber))
typedef struct MVDependencies
{
uint32 magic; /* magic constant marker */
@@ -75,9 +61,6 @@ typedef struct MVDependencies
MVDependency *deps[FLEXIBLE_ARRAY_MEMBER]; /* dependencies */
} MVDependencies;
/* size of the struct excluding the deps array */
#define SizeOfDependencies (offsetof(MVDependencies, ndeps) + sizeof(uint32))
/* used to flag stats serialized to bytea */
#define STATS_MCV_MAGIC 0xE1A651C2 /* marks serialized bytea */
#define STATS_MCV_TYPE_BASIC 1 /* basic MCV list type */