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

Commit 9fd4587

Browse files
committed
Replace many MemSet calls with struct initialization
This replaces all MemSet() calls with struct initialization where that is easily and obviously possible. (For example, some cases have to worry about padding bits, so I left those.) (The same could be done with appropriate memset() calls, but this patch is part of an effort to phase out MemSet(), so it doesn't touch memset() calls.) Reviewed-by: Ranier Vilela <ranier.vf@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://www.postgresql.org/message-id/9847b13c-b785-f4e2-75c3-12ec77a3b05c@enterprisedb.com
1 parent c94ae9d commit 9fd4587

Some content is hidden

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

51 files changed

+200
-468
lines changed

contrib/amcheck/verify_heapam.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,10 @@ report_corruption_internal(Tuplestorestate *tupstore, TupleDesc tupdesc,
554554
BlockNumber blkno, OffsetNumber offnum,
555555
AttrNumber attnum, char *msg)
556556
{
557-
Datum values[HEAPCHECK_RELATION_COLS];
558-
bool nulls[HEAPCHECK_RELATION_COLS];
557+
Datum values[HEAPCHECK_RELATION_COLS] = {0};
558+
bool nulls[HEAPCHECK_RELATION_COLS] = {0};
559559
HeapTuple tuple;
560560

561-
MemSet(values, 0, sizeof(values));
562-
MemSet(nulls, 0, sizeof(nulls));
563561
values[0] = Int64GetDatum(blkno);
564562
values[1] = Int32GetDatum(offnum);
565563
values[2] = Int32GetDatum(attnum);

contrib/bloom/blcost.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ blcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
2626
double *indexPages)
2727
{
2828
IndexOptInfo *index = path->indexinfo;
29-
GenericCosts costs;
30-
31-
MemSet(&costs, 0, sizeof(costs));
29+
GenericCosts costs = {0};
3230

3331
/* We have to visit all index tuples anyway */
3432
costs.numIndexTuples = index->tuples;

contrib/pageinspect/brinfuncs.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ brin_page_items(PG_FUNCTION_ARGS)
202202
for (;;)
203203
{
204204
Datum values[7];
205-
bool nulls[7];
205+
bool nulls[7] = {0};
206206

207207
/*
208208
* This loop is called once for every attribute of every tuple in the
@@ -230,8 +230,6 @@ brin_page_items(PG_FUNCTION_ARGS)
230230
else
231231
attno++;
232232

233-
MemSet(nulls, 0, sizeof(nulls));
234-
235233
if (unusedItem)
236234
{
237235
values[0] = UInt16GetDatum(offset);
@@ -334,7 +332,7 @@ brin_metapage_info(PG_FUNCTION_ARGS)
334332
BrinMetaPageData *meta;
335333
TupleDesc tupdesc;
336334
Datum values[4];
337-
bool nulls[4];
335+
bool nulls[4] = {0};
338336
HeapTuple htup;
339337

340338
if (!superuser())
@@ -354,7 +352,6 @@ brin_metapage_info(PG_FUNCTION_ARGS)
354352

355353
/* Extract values from the metapage */
356354
meta = (BrinMetaPageData *) PageGetContents(page);
357-
MemSet(nulls, 0, sizeof(nulls));
358355
values[0] = CStringGetTextDatum(psprintf("0x%08X", meta->brinMagic));
359356
values[1] = Int32GetDatum(meta->brinVersion);
360357
values[2] = Int32GetDatum(meta->pagesPerRange);

contrib/pageinspect/hashfuncs.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ hash_page_stats(PG_FUNCTION_ARGS)
238238
Page page;
239239
int j;
240240
Datum values[9];
241-
bool nulls[9];
241+
bool nulls[9] = {0};
242242
HashPageStat stat;
243243
HeapTuple tuple;
244244
TupleDesc tupleDesc;
@@ -261,8 +261,6 @@ hash_page_stats(PG_FUNCTION_ARGS)
261261
elog(ERROR, "return type must be a row type");
262262
tupleDesc = BlessTupleDesc(tupleDesc);
263263

264-
MemSet(nulls, 0, sizeof(nulls));
265-
266264
j = 0;
267265
values[j++] = Int32GetDatum(stat.live_items);
268266
values[j++] = Int32GetDatum(stat.dead_items);
@@ -303,7 +301,7 @@ hash_page_items(PG_FUNCTION_ARGS)
303301
Page page;
304302
Datum result;
305303
Datum values[3];
306-
bool nulls[3];
304+
bool nulls[3] = {0};
307305
uint32 hashkey;
308306
HeapTuple tuple;
309307
FuncCallContext *fctx;
@@ -361,8 +359,6 @@ hash_page_items(PG_FUNCTION_ARGS)
361359

362360
itup = (IndexTuple) PageGetItem(uargs->page, id);
363361

364-
MemSet(nulls, 0, sizeof(nulls));
365-
366362
j = 0;
367363
values[j++] = Int32GetDatum((int32) uargs->offset);
368364
values[j++] = PointerGetDatum(&itup->t_tid);
@@ -409,7 +405,7 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
409405
int i,
410406
j;
411407
Datum values[3];
412-
bool nulls[3];
408+
bool nulls[3] = {0};
413409
uint32 *freep;
414410

415411
if (!superuser())
@@ -495,8 +491,6 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
495491
elog(ERROR, "return type must be a row type");
496492
tupleDesc = BlessTupleDesc(tupleDesc);
497493

498-
MemSet(nulls, 0, sizeof(nulls));
499-
500494
j = 0;
501495
values[j++] = Int64GetDatum((int64) bitmapblkno);
502496
values[j++] = Int32GetDatum(bitmapbit);
@@ -526,7 +520,7 @@ hash_metapage_info(PG_FUNCTION_ARGS)
526520
int i,
527521
j;
528522
Datum values[16];
529-
bool nulls[16];
523+
bool nulls[16] = {0};
530524
Datum spares[HASH_MAX_SPLITPOINTS];
531525
Datum mapp[HASH_MAX_BITMAPS];
532526

@@ -544,8 +538,6 @@ hash_metapage_info(PG_FUNCTION_ARGS)
544538

545539
metad = HashPageGetMeta(page);
546540

547-
MemSet(nulls, 0, sizeof(nulls));
548-
549541
j = 0;
550542
values[j++] = Int64GetDatum((int64) metad->hashm_magic);
551543
values[j++] = Int64GetDatum((int64) metad->hashm_version);

contrib/pageinspect/heapfuncs.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ Datum
507507
heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
508508
{
509509
#define HEAP_TUPLE_INFOMASK_COLS 2
510-
Datum values[HEAP_TUPLE_INFOMASK_COLS];
511-
bool nulls[HEAP_TUPLE_INFOMASK_COLS];
510+
Datum values[HEAP_TUPLE_INFOMASK_COLS] = {0};
511+
bool nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
512512
uint16 t_infomask = PG_GETARG_INT16(0);
513513
uint16 t_infomask2 = PG_GETARG_INT16(1);
514514
int cnt = 0;
@@ -530,10 +530,6 @@ heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
530530
bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
531531
pg_popcount((const char *) &t_infomask2, sizeof(uint16));
532532

533-
/* Initialize values and NULL flags arrays */
534-
MemSet(values, 0, sizeof(values));
535-
MemSet(nulls, 0, sizeof(nulls));
536-
537533
/* If no flags, return a set of empty arrays */
538534
if (bitcnt <= 0)
539535
{

contrib/pg_prewarm/autoprewarm.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -814,12 +814,11 @@ apw_detach_shmem(int code, Datum arg)
814814
static void
815815
apw_start_leader_worker(void)
816816
{
817-
BackgroundWorker worker;
817+
BackgroundWorker worker = {0};
818818
BackgroundWorkerHandle *handle;
819819
BgwHandleStatus status;
820820
pid_t pid;
821821

822-
MemSet(&worker, 0, sizeof(BackgroundWorker));
823822
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
824823
worker.bgw_start_time = BgWorkerStart_ConsistentState;
825824
strcpy(worker.bgw_library_name, "pg_prewarm");
@@ -856,10 +855,9 @@ apw_start_leader_worker(void)
856855
static void
857856
apw_start_database_worker(void)
858857
{
859-
BackgroundWorker worker;
858+
BackgroundWorker worker = {0};
860859
BackgroundWorkerHandle *handle;
861860

862-
MemSet(&worker, 0, sizeof(BackgroundWorker));
863861
worker.bgw_flags =
864862
BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
865863
worker.bgw_start_time = BgWorkerStart_ConsistentState;

contrib/pg_stat_statements/pg_stat_statements.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -1854,8 +1854,8 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
18541854
{
18551855
pgssGlobalStats stats;
18561856
TupleDesc tupdesc;
1857-
Datum values[PG_STAT_STATEMENTS_INFO_COLS];
1858-
bool nulls[PG_STAT_STATEMENTS_INFO_COLS];
1857+
Datum values[PG_STAT_STATEMENTS_INFO_COLS] = {0};
1858+
bool nulls[PG_STAT_STATEMENTS_INFO_COLS] = {0};
18591859

18601860
if (!pgss || !pgss_hash)
18611861
ereport(ERROR,
@@ -1866,9 +1866,6 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
18661866
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
18671867
elog(ERROR, "return type must be a row type");
18681868

1869-
MemSet(values, 0, sizeof(values));
1870-
MemSet(nulls, 0, sizeof(nulls));
1871-
18721869
/* Read global statistics for pg_stat_statements */
18731870
{
18741871
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;

contrib/pg_visibility/pg_visibility.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pg_visibility_map(PG_FUNCTION_ARGS)
7575
Buffer vmbuffer = InvalidBuffer;
7676
TupleDesc tupdesc;
7777
Datum values[2];
78-
bool nulls[2];
78+
bool nulls[2] = {0};
7979

8080
rel = relation_open(relid, AccessShareLock);
8181

@@ -88,7 +88,6 @@ pg_visibility_map(PG_FUNCTION_ARGS)
8888
errmsg("invalid block number")));
8989

9090
tupdesc = pg_visibility_tupdesc(false, false);
91-
MemSet(nulls, 0, sizeof(nulls));
9291

9392
mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
9493
if (vmbuffer != InvalidBuffer)
@@ -117,7 +116,7 @@ pg_visibility(PG_FUNCTION_ARGS)
117116
Page page;
118117
TupleDesc tupdesc;
119118
Datum values[3];
120-
bool nulls[3];
119+
bool nulls[3] = {0};
121120

122121
rel = relation_open(relid, AccessShareLock);
123122

@@ -130,7 +129,6 @@ pg_visibility(PG_FUNCTION_ARGS)
130129
errmsg("invalid block number")));
131130

132131
tupdesc = pg_visibility_tupdesc(false, true);
133-
MemSet(nulls, 0, sizeof(nulls));
134132

135133
mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
136134
if (vmbuffer != InvalidBuffer)
@@ -188,10 +186,9 @@ pg_visibility_map_rel(PG_FUNCTION_ARGS)
188186
if (info->next < info->count)
189187
{
190188
Datum values[3];
191-
bool nulls[3];
189+
bool nulls[3] = {0};
192190
HeapTuple tuple;
193191

194-
MemSet(nulls, 0, sizeof(nulls));
195192
values[0] = Int64GetDatum(info->next);
196193
values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
197194
values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
@@ -233,10 +230,9 @@ pg_visibility_rel(PG_FUNCTION_ARGS)
233230
if (info->next < info->count)
234231
{
235232
Datum values[4];
236-
bool nulls[4];
233+
bool nulls[4] = {0};
237234
HeapTuple tuple;
238235

239-
MemSet(nulls, 0, sizeof(nulls));
240236
values[0] = Int64GetDatum(info->next);
241237
values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
242238
values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
@@ -266,7 +262,7 @@ pg_visibility_map_summary(PG_FUNCTION_ARGS)
266262
int64 all_frozen = 0;
267263
TupleDesc tupdesc;
268264
Datum values[2];
269-
bool nulls[2];
265+
bool nulls[2] = {0};
270266

271267
rel = relation_open(relid, AccessShareLock);
272268

@@ -300,7 +296,6 @@ pg_visibility_map_summary(PG_FUNCTION_ARGS)
300296
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "all_frozen", INT8OID, -1, 0);
301297
tupdesc = BlessTupleDesc(tupdesc);
302298

303-
MemSet(nulls, 0, sizeof(nulls));
304299
values[0] = Int64GetDatum(all_visible);
305300
values[1] = Int64GetDatum(all_frozen);
306301

contrib/pg_walinspect/pg_walinspect.c

+7-18
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ pg_get_wal_record_info(PG_FUNCTION_ARGS)
229229
{
230230
#define PG_GET_WAL_RECORD_INFO_COLS 11
231231
Datum result;
232-
Datum values[PG_GET_WAL_RECORD_INFO_COLS];
233-
bool nulls[PG_GET_WAL_RECORD_INFO_COLS];
232+
Datum values[PG_GET_WAL_RECORD_INFO_COLS] = {0};
233+
bool nulls[PG_GET_WAL_RECORD_INFO_COLS] = {0};
234234
XLogRecPtr lsn;
235235
XLogRecPtr curr_lsn;
236236
XLogRecPtr first_record;
@@ -266,9 +266,6 @@ pg_get_wal_record_info(PG_FUNCTION_ARGS)
266266
errmsg("could not read WAL at %X/%X",
267267
LSN_FORMAT_ARGS(first_record))));
268268

269-
MemSet(values, 0, sizeof(values));
270-
MemSet(nulls, 0, sizeof(nulls));
271-
272269
GetWALRecordInfo(xlogreader, first_record, values, nulls,
273270
PG_GET_WAL_RECORD_INFO_COLS);
274271

@@ -334,18 +331,15 @@ GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
334331
XLogRecPtr first_record;
335332
XLogReaderState *xlogreader;
336333
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
337-
Datum values[PG_GET_WAL_RECORDS_INFO_COLS];
338-
bool nulls[PG_GET_WAL_RECORDS_INFO_COLS];
334+
Datum values[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
335+
bool nulls[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
339336

340337
SetSingleFuncCall(fcinfo, 0);
341338

342339
xlogreader = InitXLogReaderState(start_lsn, &first_record);
343340

344341
Assert(xlogreader);
345342

346-
MemSet(values, 0, sizeof(values));
347-
MemSet(nulls, 0, sizeof(nulls));
348-
349343
while (ReadNextXLogRecord(xlogreader, first_record) &&
350344
xlogreader->EndRecPtr <= end_lsn)
351345
{
@@ -556,17 +550,15 @@ GetWalStats(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
556550
#define PG_GET_WAL_STATS_COLS 9
557551
XLogRecPtr first_record;
558552
XLogReaderState *xlogreader;
559-
XLogStats stats;
553+
XLogStats stats = {0};
560554
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
561-
Datum values[PG_GET_WAL_STATS_COLS];
562-
bool nulls[PG_GET_WAL_STATS_COLS];
555+
Datum values[PG_GET_WAL_STATS_COLS] = {0};
556+
bool nulls[PG_GET_WAL_STATS_COLS] = {0};
563557

564558
SetSingleFuncCall(fcinfo, 0);
565559

566560
xlogreader = InitXLogReaderState(start_lsn, &first_record);
567561

568-
MemSet(&stats, 0, sizeof(stats));
569-
570562
while (ReadNextXLogRecord(xlogreader, first_record) &&
571563
xlogreader->EndRecPtr <= end_lsn)
572564
{
@@ -578,9 +570,6 @@ GetWalStats(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
578570
pfree(xlogreader->private_data);
579571
XLogReaderFree(xlogreader);
580572

581-
MemSet(values, 0, sizeof(values));
582-
MemSet(nulls, 0, sizeof(nulls));
583-
584573
GetXLogSummaryStats(&stats, rsinfo, values, nulls,
585574
PG_GET_WAL_STATS_COLS,
586575
stats_per_record);

contrib/pgstattuple/pgstatindex.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ pgstathashindex(PG_FUNCTION_ARGS)
575575
HeapTuple tuple;
576576
TupleDesc tupleDesc;
577577
Datum values[8];
578-
bool nulls[8];
578+
bool nulls[8] = {0};
579579
Buffer metabuf;
580580
HashMetaPage metap;
581581
float8 free_percent;
@@ -697,7 +697,6 @@ pgstathashindex(PG_FUNCTION_ARGS)
697697
/*
698698
* Build and return the tuple
699699
*/
700-
MemSet(nulls, 0, sizeof(nulls));
701700
values[0] = Int32GetDatum(stats.version);
702701
values[1] = Int64GetDatum((int64) stats.bucket_pages);
703702
values[2] = Int64GetDatum((int64) stats.overflow_pages);

contrib/postgres_fdw/connection.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -1678,18 +1678,15 @@ postgres_fdw_get_connections(PG_FUNCTION_ARGS)
16781678
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
16791679
{
16801680
ForeignServer *server;
1681-
Datum values[POSTGRES_FDW_GET_CONNECTIONS_COLS];
1682-
bool nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS];
1681+
Datum values[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
1682+
bool nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
16831683

16841684
/* We only look for open remote connections */
16851685
if (!entry->conn)
16861686
continue;
16871687

16881688
server = GetForeignServerExtended(entry->serverid, FSV_MISSING_OK);
16891689

1690-
MemSet(values, 0, sizeof(values));
1691-
MemSet(nulls, 0, sizeof(nulls));
1692-
16931690
/*
16941691
* The foreign server may have been dropped in current explicit
16951692
* transaction. It is not possible to drop the server from another

0 commit comments

Comments
 (0)