Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2e21121

Browse files
committed
Use FLEXIBLE_ARRAY_MEMBER in a number of other places.
I think we're about done with this...
1 parent e1a11d9 commit 2e21121

File tree

14 files changed

+54
-62
lines changed

14 files changed

+54
-62
lines changed

contrib/hstore/hstore_gist.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct
4141
{
4242
int32 vl_len_; /* varlena header (do not touch directly!) */
4343
int32 flag;
44-
char data[1];
44+
char data[FLEXIBLE_ARRAY_MEMBER];
4545
} GISTTYPE;
4646

4747
#define ALLISTRUE 0x04

contrib/spi/timetravel.c

+16-9
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ static int nPlans = 0;
3535
typedef struct _TTOffList
3636
{
3737
struct _TTOffList *next;
38-
char name[1];
38+
char name[FLEXIBLE_ARRAY_MEMBER];
3939
} TTOffList;
4040

41-
static TTOffList TTOff = {NULL, {0}};
41+
static TTOffList *TTOff = NULL;
4242

4343
static int findTTStatus(char *name);
4444
static EPlan *find_plan(char *ident, EPlan **eplan, int *nplans);
@@ -428,10 +428,11 @@ set_timetravel(PG_FUNCTION_ARGS)
428428
char *d;
429429
char *s;
430430
int32 ret;
431-
TTOffList *p,
431+
TTOffList *prev,
432432
*pp;
433433

434-
for (pp = (p = &TTOff)->next; pp; pp = (p = pp)->next)
434+
prev = NULL;
435+
for (pp = TTOff; pp; prev = pp, pp = pp->next)
435436
{
436437
if (namestrcmp(relname, pp->name) == 0)
437438
break;
@@ -442,7 +443,10 @@ set_timetravel(PG_FUNCTION_ARGS)
442443
if (on != 0)
443444
{
444445
/* turn ON */
445-
p->next = pp->next;
446+
if (prev)
447+
prev->next = pp->next;
448+
else
449+
TTOff = pp->next;
446450
free(pp);
447451
}
448452
ret = 0;
@@ -456,15 +460,18 @@ set_timetravel(PG_FUNCTION_ARGS)
456460
s = rname = DatumGetCString(DirectFunctionCall1(nameout, NameGetDatum(relname)));
457461
if (s)
458462
{
459-
pp = malloc(sizeof(TTOffList) + strlen(rname));
463+
pp = malloc(offsetof(TTOffList, name) +strlen(rname) + 1);
460464
if (pp)
461465
{
462466
pp->next = NULL;
463-
p->next = pp;
464467
d = pp->name;
465468
while (*s)
466469
*d++ = tolower((unsigned char) *s++);
467470
*d = '\0';
471+
if (prev)
472+
prev->next = pp;
473+
else
474+
TTOff = pp;
468475
}
469476
pfree(rname);
470477
}
@@ -486,7 +493,7 @@ get_timetravel(PG_FUNCTION_ARGS)
486493
Name relname = PG_GETARG_NAME(0);
487494
TTOffList *pp;
488495

489-
for (pp = TTOff.next; pp; pp = pp->next)
496+
for (pp = TTOff; pp; pp = pp->next)
490497
{
491498
if (namestrcmp(relname, pp->name) == 0)
492499
PG_RETURN_INT32(0);
@@ -499,7 +506,7 @@ findTTStatus(char *name)
499506
{
500507
TTOffList *pp;
501508

502-
for (pp = TTOff.next; pp; pp = pp->next)
509+
for (pp = TTOff; pp; pp = pp->next)
503510
if (pg_strcasecmp(name, pp->name) == 0)
504511
return 0;
505512
return 1;

src/backend/access/heap/syncscan.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ typedef struct ss_scan_locations_t
103103
{
104104
ss_lru_item_t *head;
105105
ss_lru_item_t *tail;
106-
ss_lru_item_t items[1]; /* SYNC_SCAN_NELEM items */
106+
ss_lru_item_t items[FLEXIBLE_ARRAY_MEMBER]; /* SYNC_SCAN_NELEM items */
107107
} ss_scan_locations_t;
108108

109-
#define SizeOfScanLocations(N) offsetof(ss_scan_locations_t, items[N])
109+
#define SizeOfScanLocations(N) \
110+
(offsetof(ss_scan_locations_t, items) + (N) * sizeof(ss_lru_item_t))
110111

111112
/* Pointer to struct in shared memory */
112113
static ss_scan_locations_t *scan_locations;

src/backend/commands/async.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ typedef struct AsyncQueueControl
237237
QueuePosition tail; /* the global tail is equivalent to the tail
238238
* of the "slowest" backend */
239239
TimestampTz lastQueueFillWarn; /* time of last queue-full msg */
240-
QueueBackendStatus backend[1]; /* actually of length MaxBackends+1 */
241-
/* DO NOT ADD FURTHER STRUCT MEMBERS HERE */
240+
QueueBackendStatus backend[FLEXIBLE_ARRAY_MEMBER];
241+
/* backend[0] is not used; used entries are from [1] to [MaxBackends] */
242242
} AsyncQueueControl;
243243

244244
static AsyncQueueControl *asyncQueueControl;
@@ -303,7 +303,7 @@ typedef enum
303303
typedef struct
304304
{
305305
ListenActionKind action;
306-
char channel[1]; /* actually, as long as needed */
306+
char channel[FLEXIBLE_ARRAY_MEMBER]; /* nul-terminated string */
307307
} ListenAction;
308308

309309
static List *pendingActions = NIL; /* list of ListenAction */
@@ -417,8 +417,8 @@ AsyncShmemSize(void)
417417
Size size;
418418

419419
/* This had better match AsyncShmemInit */
420-
size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
421-
size = add_size(size, sizeof(AsyncQueueControl));
420+
size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
421+
size = add_size(size, offsetof(AsyncQueueControl, backend));
422422

423423
size = add_size(size, SimpleLruShmemSize(NUM_ASYNC_BUFFERS, 0));
424424

@@ -438,12 +438,11 @@ AsyncShmemInit(void)
438438
/*
439439
* Create or attach to the AsyncQueueControl structure.
440440
*
441-
* The used entries in the backend[] array run from 1 to MaxBackends.
442-
* sizeof(AsyncQueueControl) already includes space for the unused zero'th
443-
* entry, but we need to add on space for the used entries.
441+
* The used entries in the backend[] array run from 1 to MaxBackends; the
442+
* zero'th entry is unused but must be allocated.
444443
*/
445-
size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
446-
size = add_size(size, sizeof(AsyncQueueControl));
444+
size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
445+
size = add_size(size, offsetof(AsyncQueueControl, backend));
447446

448447
asyncQueueControl = (AsyncQueueControl *)
449448
ShmemInitStruct("Async Queue Control", size, &found);
@@ -605,7 +604,8 @@ queue_listen(ListenActionKind action, const char *channel)
605604
oldcontext = MemoryContextSwitchTo(CurTransactionContext);
606605

607606
/* space for terminating null is included in sizeof(ListenAction) */
608-
actrec = (ListenAction *) palloc(sizeof(ListenAction) + strlen(channel));
607+
actrec = (ListenAction *) palloc(offsetof(ListenAction, channel) +
608+
strlen(channel) + 1);
609609
actrec->action = action;
610610
strcpy(actrec->channel, channel);
611611

src/backend/libpq/auth.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ typedef struct
21722172
{
21732173
uint8 attribute;
21742174
uint8 length;
2175-
uint8 data[1];
2175+
uint8 data[FLEXIBLE_ARRAY_MEMBER];
21762176
} radius_attribute;
21772177

21782178
typedef struct
@@ -2220,7 +2220,6 @@ radius_add_attribute(radius_packet *packet, uint8 type, const unsigned char *dat
22202220
"Adding attribute code %d with length %d to radius packet would create oversize packet, ignoring",
22212221
type, len);
22222222
return;
2223-
22242223
}
22252224

22262225
attr = (radius_attribute *) ((unsigned char *) packet + packet->length);

src/backend/storage/buffer/freelist.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ typedef struct BufferAccessStrategyData
9393
* simplicity this is palloc'd together with the fixed fields of the
9494
* struct.
9595
*/
96-
Buffer buffers[1]; /* VARIABLE SIZE ARRAY */
96+
Buffer buffers[FLEXIBLE_ARRAY_MEMBER];
9797
} BufferAccessStrategyData;
9898

9999

src/backend/storage/ipc/sinvaladt.c

+3-10
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,9 @@ typedef struct SISeg
184184
SharedInvalidationMessage buffer[MAXNUMMESSAGES];
185185

186186
/*
187-
* Per-backend state info.
188-
*
189-
* We declare procState as 1 entry because C wants a fixed-size array, but
190-
* actually it is maxBackends entries long.
187+
* Per-backend invalidation state info (has MaxBackends entries).
191188
*/
192-
ProcState procState[1]; /* reflects the invalidation state */
189+
ProcState procState[FLEXIBLE_ARRAY_MEMBER];
193190
} SISeg;
194191

195192
static SISeg *shmInvalBuffer; /* pointer to the shared inval buffer */
@@ -221,16 +218,12 @@ SInvalShmemSize(void)
221218
void
222219
CreateSharedInvalidationState(void)
223220
{
224-
Size size;
225221
int i;
226222
bool found;
227223

228224
/* Allocate space in shared memory */
229-
size = offsetof(SISeg, procState);
230-
size = add_size(size, mul_size(sizeof(ProcState), MaxBackends));
231-
232225
shmInvalBuffer = (SISeg *)
233-
ShmemInitStruct("shmInvalBuffer", size, &found);
226+
ShmemInitStruct("shmInvalBuffer", SInvalShmemSize(), &found);
234227
if (found)
235228
return;
236229

src/backend/utils/adt/numeric.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ typedef int16 NumericDigit;
123123
struct NumericShort
124124
{
125125
uint16 n_header; /* Sign + display scale + weight */
126-
NumericDigit n_data[1]; /* Digits */
126+
NumericDigit n_data[FLEXIBLE_ARRAY_MEMBER]; /* Digits */
127127
};
128128

129129
struct NumericLong
130130
{
131131
uint16 n_sign_dscale; /* Sign + display scale */
132132
int16 n_weight; /* Weight of 1st digit */
133-
NumericDigit n_data[1]; /* Digits */
133+
NumericDigit n_data[FLEXIBLE_ARRAY_MEMBER]; /* Digits */
134134
};
135135

136136
union NumericChoice
@@ -1262,7 +1262,7 @@ numeric_floor(PG_FUNCTION_ARGS)
12621262
/*
12631263
* generate_series_numeric() -
12641264
*
1265-
* Generate series of numeric.
1265+
* Generate series of numeric.
12661266
*/
12671267
Datum
12681268
generate_series_numeric(PG_FUNCTION_ARGS)
@@ -1297,7 +1297,7 @@ generate_series_step_numeric(PG_FUNCTION_ARGS)
12971297
/* see if we were given an explicit step size */
12981298
if (PG_NARGS() == 3)
12991299
{
1300-
Numeric step_num = PG_GETARG_NUMERIC(2);
1300+
Numeric step_num = PG_GETARG_NUMERIC(2);
13011301

13021302
if (NUMERIC_IS_NAN(step_num))
13031303
ereport(ERROR,
@@ -1356,7 +1356,7 @@ generate_series_step_numeric(PG_FUNCTION_ARGS)
13561356
(fctx->step.sign == NUMERIC_NEG &&
13571357
cmp_var(&fctx->current, &fctx->stop) >= 0))
13581358
{
1359-
Numeric result = make_result(&fctx->current);
1359+
Numeric result = make_result(&fctx->current);
13601360

13611361
/* switch to memory context appropriate for iteration calculation */
13621362
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

src/backend/utils/adt/tsgistidx.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct
5050
{
5151
int32 vl_len_; /* varlena header (do not touch directly!) */
5252
int32 flag;
53-
char data[1];
53+
char data[FLEXIBLE_ARRAY_MEMBER];
5454
} SignTSVector;
5555

5656
#define ARRKEY 0x01

src/backend/utils/adt/tsvector_op.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef struct StatEntry
4444
struct StatEntry *left;
4545
struct StatEntry *right;
4646
uint32 lenlexeme;
47-
char lexeme[1];
47+
char lexeme[FLEXIBLE_ARRAY_MEMBER];
4848
} StatEntry;
4949

5050
#define STATENTRYHDRSZ (offsetof(StatEntry, lexeme))

src/backend/utils/adt/txid.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ typedef struct
6464
uint32 nxip; /* number of txids in xip array */
6565
txid xmin;
6666
txid xmax;
67-
txid xip[1]; /* in-progress txids, xmin <= xip[i] < xmax */
67+
/* in-progress txids, xmin <= xip[i] < xmax: */
68+
txid xip[FLEXIBLE_ARRAY_MEMBER];
6869
} TxidSnapshot;
6970

7071
#define TXID_SNAPSHOT_SIZE(nxip) \

src/backend/utils/fmgr/dfmgr.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ typedef struct df_files
5151
ino_t inode; /* Inode number of file */
5252
#endif
5353
void *handle; /* a handle for pg_dl* functions */
54-
char filename[1]; /* Full pathname of file */
55-
56-
/*
57-
* we allocate the block big enough for actual length of pathname.
58-
* filename[] must be last item in struct!
59-
*/
54+
char filename[FLEXIBLE_ARRAY_MEMBER]; /* Full pathname of file */
6055
} DynamicFileList;
6156

6257
static DynamicFileList *file_list = NULL;
@@ -217,13 +212,13 @@ internal_load_library(const char *libname)
217212
* File not loaded yet.
218213
*/
219214
file_scanner = (DynamicFileList *)
220-
malloc(sizeof(DynamicFileList) + strlen(libname));
215+
malloc(offsetof(DynamicFileList, filename) +strlen(libname) + 1);
221216
if (file_scanner == NULL)
222217
ereport(ERROR,
223218
(errcode(ERRCODE_OUT_OF_MEMORY),
224219
errmsg("out of memory")));
225220

226-
MemSet(file_scanner, 0, sizeof(DynamicFileList));
221+
MemSet(file_scanner, 0, offsetof(DynamicFileList, filename));
227222
strcpy(file_scanner->filename, libname);
228223
file_scanner->device = stat_buf.st_dev;
229224
#ifndef WIN32

src/backend/utils/sort/logtape.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,9 @@ struct LogicalTapeSet
166166
int nFreeBlocks; /* # of currently free blocks */
167167
int freeBlocksLen; /* current allocated length of freeBlocks[] */
168168

169-
/*
170-
* tapes[] is declared size 1 since C wants a fixed size, but actually it
171-
* is of length nTapes.
172-
*/
169+
/* The array of logical tapes. */
173170
int nTapes; /* # of logical tapes in set */
174-
LogicalTape tapes[1]; /* must be last in struct! */
171+
LogicalTape tapes[FLEXIBLE_ARRAY_MEMBER]; /* has nTapes nentries */
175172
};
176173

177174
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer);
@@ -519,12 +516,11 @@ LogicalTapeSetCreate(int ntapes)
519516
int i;
520517

521518
/*
522-
* Create top-level struct including per-tape LogicalTape structs. First
523-
* LogicalTape struct is already counted in sizeof(LogicalTapeSet).
519+
* Create top-level struct including per-tape LogicalTape structs.
524520
*/
525521
Assert(ntapes > 0);
526-
lts = (LogicalTapeSet *) palloc(sizeof(LogicalTapeSet) +
527-
(ntapes - 1) *sizeof(LogicalTape));
522+
lts = (LogicalTapeSet *) palloc(offsetof(LogicalTapeSet, tapes) +
523+
ntapes * sizeof(LogicalTape));
528524
lts->pfile = BufFileCreateTemp(false);
529525
lts->nFileBlocks = 0L;
530526
lts->forgetFreeSpace = false;

src/interfaces/ecpg/ecpglib/extern.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ enum ARRAY_TYPE
3333
struct ECPGgeneric_varchar
3434
{
3535
int len;
36-
char arr[1];
36+
char arr[FLEXIBLE_ARRAY_MEMBER];
3737
};
3838

3939
/*

0 commit comments

Comments
 (0)