Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit09d8d11

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 boguswarnings from static code analyzers and perhaps compiler misoptimizations.This patch is just a down payment on eliminating the whole problem, butit gets rid of a lot of easy-to-fix cases.Note that the main problem with doing this is that one must no longer relyon computing sizeof(the containing struct), since the result would becompiler-dependent. Instead use offsetof(struct, lastfield). Autoconfalso warns against spelling that offsetof(struct, lastfield[0]).Michael Paquier, review and additional fixes by me.
1 parent2fb7a75 commit09d8d11

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
unsignedintheader;
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-
doublex[1];
29+
doublex[FLEXIBLE_ARRAY_MEMBER];
3130
}NDBOX;
3231

3332
#definePOINT_BIT0x80000000
@@ -41,9 +40,9 @@ typedef struct NDBOX
4140
#defineLL_COORD(cube,i) ( (cube)->x[i] )
4241
#defineUR_COORD(cube,i) ( IS_POINT(cube) ? (cube)->x[i] : (cube)->x[(i) + DIM(cube)] )
4342

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

47-
#defineDatumGetNDBOX(x)((NDBOX*)DatumGetPointer(x))
48-
#definePG_GETARG_NDBOX(x)DatumGetNDBOX( PG_DETOAST_DATUM(PG_GETARG_DATUM(x)))
46+
#defineDatumGetNDBOX(x)((NDBOX *) PG_DETOAST_DATUM(x))
47+
#definePG_GETARG_NDBOX(x)DatumGetNDBOX(PG_GETARG_DATUM(x))
4948
#definePG_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
int32vl_len_;/* varlena header (do not touch directly!) */
7575
int32flag;
76-
chardata[1];
76+
chardata[FLEXIBLE_ARRAY_MEMBER];
7777
}GISTTYPE;
7878

7979
#defineALLISTRUE0x04
@@ -133,7 +133,7 @@ typedef struct QUERYTYPE
133133
{
134134
int32vl_len_;/* varlena header (do not touch directly!) */
135135
int32size;/* number of ITEMs */
136-
ITEMitems[1];/* variable length array */
136+
ITEMitems[FLEXIBLE_ARRAY_MEMBER];
137137
}QUERYTYPE;
138138

139139
#defineHDRSIZEQToffsetof(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
typedefstruct
1111
{
1212
uint16len;
13-
charname[1];
13+
charname[FLEXIBLE_ARRAY_MEMBER];
1414
}ltree_level;
1515

1616
#defineLEVEL_HDRSIZE(offsetof(ltree_level,name))
@@ -20,7 +20,7 @@ typedef struct
2020
{
2121
int32vl_len_;/* varlena header (do not touch directly!) */
2222
uint16numlevel;
23-
chardata[1];
23+
chardata[FLEXIBLE_ARRAY_MEMBER];
2424
}ltree;
2525

2626
#defineLTREE_HDRSIZEMAXALIGN( offsetof(ltree, data) )
@@ -34,7 +34,7 @@ typedef struct
3434
int32val;
3535
uint16len;
3636
uint8flag;
37-
charname[1];
37+
charname[FLEXIBLE_ARRAY_MEMBER];
3838
}lquery_variant;
3939

4040
#defineLVAR_HDRSIZE MAXALIGN(offsetof(lquery_variant, name))
@@ -51,7 +51,7 @@ typedef struct
5151
uint16numvar;
5252
uint16low;
5353
uint16high;
54-
charvariants[1];
54+
charvariants[FLEXIBLE_ARRAY_MEMBER];
5555
}lquery_level;
5656

5757
#defineLQL_HDRSIZE MAXALIGN( offsetof(lquery_level,variants) )
@@ -72,7 +72,7 @@ typedef struct
7272
uint16numlevel;
7373
uint16firstgood;
7474
uint16flag;
75-
chardata[1];
75+
chardata[FLEXIBLE_ARRAY_MEMBER];
7676
}lquery;
7777

7878
#defineLQUERY_HDRSIZE MAXALIGN( offsetof(lquery, data) )
@@ -107,7 +107,7 @@ typedef struct
107107
{
108108
int32vl_len_;/* varlena header (do not touch directly!) */
109109
int32size;
110-
chardata[1];
110+
chardata[FLEXIBLE_ARRAY_MEMBER];
111111
}ltxtquery;
112112

113113
#defineHDRSIZEQTMAXALIGN(VARHDRSZ + sizeof(int32))
@@ -208,7 +208,7 @@ typedef struct
208208
{
209209
int32vl_len_;/* varlena header (do not touch directly!) */
210210
uint32flag;
211-
chardata[1];
211+
chardata[FLEXIBLE_ARRAY_MEMBER];
212212
}ltree_gist;
213213

214214
#defineLTG_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
int32vl_len_;/* varlena header (do not touch directly!) */
6565
uint8flag;
66-
chardata[1];
66+
chardata[FLEXIBLE_ARRAY_MEMBER];
6767
}TRGM;
6868

6969
#defineTRGMHDRSIZE (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-
*sowe may fail to notice a change. We could protect against that case
266-
*bycalling 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-
#defineSPACE_PER_OP MAXALIGN(sizeof(struct _FuncCandidateList) + sizeof(Oid))
1600+
#defineSPACE_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
inti;
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
returnNULL;
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)

[8]ページ先頭

©2009-2025 Movatter.jp