Skip to content

Commit 09d8d11

Browse files
committed
Use FLEXIBLE_ARRAY_MEMBER in a bunch more places.
Replace some bogus "x[1]" declarations with "x[FLEXIBLE_ARRAY_MEMBER]". Aside from being more self-documenting, this should help prevent bogus warnings from static code analyzers and perhaps compiler misoptimizations. This patch is just a down payment on eliminating the whole problem, but it gets rid of a lot of easy-to-fix cases. Note that the main problem with doing this is that one must no longer rely on computing sizeof(the containing struct), since the result would be compiler-dependent. Instead use offsetof(struct, lastfield). Autoconf also warns against spelling that offsetof(struct, lastfield[0]). Michael Paquier, review and additional fixes by me.
1 parent 2fb7a75 commit 09d8d11

File tree

44 files changed

+109
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+109
-127
lines changed

contrib/cube/cubedata.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ typedef struct NDBOX
2323
unsigned int header;
2424

2525
/*
26-
* Variable length array. The lower left coordinates for each dimension
27-
* come first, followed by upper right coordinates unless the point flag
28-
* is set.
26+
* The lower left coordinates for each dimension come first, followed by
27+
* upper right coordinates unless the point flag is set.
2928
*/
30-
double x[1];
29+
double x[FLEXIBLE_ARRAY_MEMBER];
3130
} NDBOX;
3231

3332
#define POINT_BIT 0x80000000
@@ -41,9 +40,9 @@ typedef struct NDBOX
4140
#define LL_COORD(cube, i) ( (cube)->x[i] )
4241
#define UR_COORD(cube, i) ( IS_POINT(cube) ? (cube)->x[i] : (cube)->x[(i) + DIM(cube)] )
4342

44-
#define POINT_SIZE(_dim) (offsetof(NDBOX, x[0]) + sizeof(double)*(_dim))
45-
#define CUBE_SIZE(_dim) (offsetof(NDBOX, x[0]) + sizeof(double)*(_dim)*2)
43+
#define POINT_SIZE(_dim) (offsetof(NDBOX, x) + sizeof(double)*(_dim))
44+
#define CUBE_SIZE(_dim) (offsetof(NDBOX, x) + sizeof(double)*(_dim)*2)
4645

47-
#define DatumGetNDBOX(x) ((NDBOX*)DatumGetPointer(x))
48-
#define PG_GETARG_NDBOX(x) DatumGetNDBOX( PG_DETOAST_DATUM(PG_GETARG_DATUM(x)) )
46+
#define DatumGetNDBOX(x) ((NDBOX *) PG_DETOAST_DATUM(x))
47+
#define PG_GETARG_NDBOX(x) DatumGetNDBOX(PG_GETARG_DATUM(x))
4948
#define PG_RETURN_NDBOX(x) PG_RETURN_POINTER(x)

contrib/intarray/_int.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ typedef struct
7373
{
7474
int32 vl_len_; /* varlena header (do not touch directly!) */
7575
int32 flag;
76-
char data[1];
76+
char data[FLEXIBLE_ARRAY_MEMBER];
7777
} GISTTYPE;
7878

7979
#define ALLISTRUE 0x04
@@ -133,7 +133,7 @@ typedef struct QUERYTYPE
133133
{
134134
int32 vl_len_; /* varlena header (do not touch directly!) */
135135
int32 size; /* number of ITEMs */
136-
ITEM items[1]; /* variable length array */
136+
ITEM items[FLEXIBLE_ARRAY_MEMBER];
137137
} QUERYTYPE;
138138

139139
#define HDRSIZEQT offsetof(QUERYTYPE, items)

contrib/ltree/ltree.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
typedef struct
1111
{
1212
uint16 len;
13-
char name[1];
13+
char name[FLEXIBLE_ARRAY_MEMBER];
1414
} ltree_level;
1515

1616
#define LEVEL_HDRSIZE (offsetof(ltree_level,name))
@@ -20,7 +20,7 @@ typedef struct
2020
{
2121
int32 vl_len_; /* varlena header (do not touch directly!) */
2222
uint16 numlevel;
23-
char data[1];
23+
char data[FLEXIBLE_ARRAY_MEMBER];
2424
} ltree;
2525

2626
#define LTREE_HDRSIZE MAXALIGN( offsetof(ltree, data) )
@@ -34,7 +34,7 @@ typedef struct
3434
int32 val;
3535
uint16 len;
3636
uint8 flag;
37-
char name[1];
37+
char name[FLEXIBLE_ARRAY_MEMBER];
3838
} lquery_variant;
3939

4040
#define LVAR_HDRSIZE MAXALIGN(offsetof(lquery_variant, name))
@@ -51,7 +51,7 @@ typedef struct
5151
uint16 numvar;
5252
uint16 low;
5353
uint16 high;
54-
char variants[1];
54+
char variants[FLEXIBLE_ARRAY_MEMBER];
5555
} lquery_level;
5656

5757
#define LQL_HDRSIZE MAXALIGN( offsetof(lquery_level,variants) )
@@ -72,7 +72,7 @@ typedef struct
7272
uint16 numlevel;
7373
uint16 firstgood;
7474
uint16 flag;
75-
char data[1];
75+
char data[FLEXIBLE_ARRAY_MEMBER];
7676
} lquery;
7777

7878
#define LQUERY_HDRSIZE MAXALIGN( offsetof(lquery, data) )
@@ -107,7 +107,7 @@ typedef struct
107107
{
108108
int32 vl_len_; /* varlena header (do not touch directly!) */
109109
int32 size;
110-
char data[1];
110+
char data[FLEXIBLE_ARRAY_MEMBER];
111111
} ltxtquery;
112112

113113
#define HDRSIZEQT MAXALIGN(VARHDRSZ + sizeof(int32))
@@ -208,7 +208,7 @@ typedef struct
208208
{
209209
int32 vl_len_; /* varlena header (do not touch directly!) */
210210
uint32 flag;
211-
char data[1];
211+
char data[FLEXIBLE_ARRAY_MEMBER];
212212
} ltree_gist;
213213

214214
#define LTG_ONENODE 0x01

contrib/pageinspect/rawpage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ page_header(PG_FUNCTION_ARGS)
192192
* Check that enough data was supplied, so that we don't try to access
193193
* fields outside the supplied buffer.
194194
*/
195-
if (raw_page_size < sizeof(PageHeaderData))
195+
if (raw_page_size < SizeOfPageHeaderData)
196196
ereport(ERROR,
197197
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
198198
errmsg("input page too small (%d bytes)", raw_page_size)));

contrib/pg_trgm/trgm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ typedef struct
6363
{
6464
int32 vl_len_; /* varlena header (do not touch directly!) */
6565
uint8 flag;
66-
char data[1];
66+
char data[FLEXIBLE_ARRAY_MEMBER];
6767
} TRGM;
6868

6969
#define TRGMHDRSIZE (VARHDRSZ + sizeof(uint8))

src/backend/catalog/namespace.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
261261
* with the answer changing under them, or that they already hold some
262262
* appropriate lock, and therefore return the first answer we get without
263263
* checking for invalidation messages. Also, if the requested lock is
264-
* already held, LockRelationOid will not AcceptInvalidationMessages,
265-
* so we may fail to notice a change. We could protect against that case
266-
* by calling AcceptInvalidationMessages() before beginning this loop, but
264+
* already held, LockRelationOid will not AcceptInvalidationMessages, so
265+
* we may fail to notice a change. We could protect against that case by
266+
* calling AcceptInvalidationMessages() before beginning this loop, but
267267
* that would add a significant amount overhead, so for now we don't.
268268
*/
269269
for (;;)
@@ -1075,8 +1075,8 @@ FuncnameGetCandidates(List *names, int nargs, List *argnames,
10751075
*/
10761076
effective_nargs = Max(pronargs, nargs);
10771077
newResult = (FuncCandidateList)
1078-
palloc(sizeof(struct _FuncCandidateList) - sizeof(Oid)
1079-
+ effective_nargs * sizeof(Oid));
1078+
palloc(offsetof(struct _FuncCandidateList, args) +
1079+
effective_nargs * sizeof(Oid));
10801080
newResult->pathpos = pathpos;
10811081
newResult->oid = HeapTupleGetOid(proctup);
10821082
newResult->nargs = effective_nargs;
@@ -1597,7 +1597,8 @@ OpernameGetCandidates(List *names, char oprkind, bool missing_schema_ok)
15971597
* separate palloc for each operator, but profiling revealed that the
15981598
* pallocs used an unreasonably large fraction of parsing time.
15991599
*/
1600-
#define SPACE_PER_OP MAXALIGN(sizeof(struct _FuncCandidateList) + sizeof(Oid))
1600+
#define SPACE_PER_OP MAXALIGN(offsetof(struct _FuncCandidateList, args) + \
1601+
2 * sizeof(Oid))
16011602

16021603
if (catlist->n_members > 0)
16031604
resultSpace = palloc(catlist->n_members * SPACE_PER_OP);

src/backend/commands/prepare.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,9 @@ EvaluateParams(PreparedStatement *pstmt, List *params,
383383
/* Prepare the expressions for execution */
384384
exprstates = (List *) ExecPrepareExpr((Expr *) params, estate);
385385

386-
/* sizeof(ParamListInfoData) includes the first array element */
387386
paramLI = (ParamListInfo)
388-
palloc(sizeof(ParamListInfoData) +
389-
(num_params - 1) * sizeof(ParamExternData));
387+
palloc(offsetof(ParamListInfoData, params) +
388+
num_params * sizeof(ParamExternData));
390389
/* we have static list of params, so no hooks needed */
391390
paramLI->paramFetch = NULL;
392391
paramLI->paramFetchArg = NULL;

src/backend/executor/functions.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,9 @@ postquel_sub_params(SQLFunctionCachePtr fcache,
896896

897897
if (fcache->paramLI == NULL)
898898
{
899-
/* sizeof(ParamListInfoData) includes the first array element */
900-
paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
901-
(nargs - 1) * sizeof(ParamExternData));
899+
paramLI = (ParamListInfo)
900+
palloc(offsetof(ParamListInfoData, params) +
901+
nargs * sizeof(ParamExternData));
902902
/* we have static list of params, so no hooks needed */
903903
paramLI->paramFetch = NULL;
904904
paramLI->paramFetchArg = NULL;

src/backend/executor/spi.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,9 +2290,8 @@ _SPI_convert_params(int nargs, Oid *argtypes,
22902290
{
22912291
int i;
22922292

2293-
/* sizeof(ParamListInfoData) includes the first array element */
2294-
paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
2295-
(nargs - 1) * sizeof(ParamExternData));
2293+
paramLI = (ParamListInfo) palloc(offsetof(ParamListInfoData, params) +
2294+
nargs * sizeof(ParamExternData));
22962295
/* we have static list of params, so no hooks needed */
22972296
paramLI->paramFetch = NULL;
22982297
paramLI->paramFetchArg = NULL;

src/backend/nodes/params.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ copyParamList(ParamListInfo from)
4040
if (from == NULL || from->numParams <= 0)
4141
return NULL;
4242

43-
/* sizeof(ParamListInfoData) includes the first array element */
44-
size = sizeof(ParamListInfoData) +
45-
(from->numParams - 1) * sizeof(ParamExternData);
43+
size = offsetof(ParamListInfoData, params) +
44+
from->numParams * sizeof(ParamExternData);
4645

4746
retval = (ParamListInfo) palloc(size);
4847
retval->paramFetch = NULL;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy