1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-24 09:27:52 +03:00

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
This commit is contained in:
Peter Eisentraut
2022-07-16 08:42:15 +02:00
parent c94ae9d827
commit 9fd45870c1
51 changed files with 200 additions and 468 deletions

View File

@@ -689,8 +689,8 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
volatile Oid dst_deftablespace;
Relation pg_database_rel;
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
Datum new_record[Natts_pg_database] = {0};
bool new_record_nulls[Natts_pg_database] = {0};
Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
@@ -1296,9 +1296,6 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
(dblocprovider != COLLPROVIDER_ICU && !dbiculocale));
/* Form tuple */
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
new_record[Anum_pg_database_oid - 1] = ObjectIdGetDatum(dboid);
new_record[Anum_pg_database_datname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(dbname));
@@ -1822,9 +1819,6 @@ movedb(const char *dbname, const char *tblspcname)
newtuple;
Oid src_tblspcoid,
dst_tblspcoid;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
bool new_record_repl[Natts_pg_database];
ScanKeyData scankey;
SysScanDesc sysscan;
AclResult aclresult;
@@ -2003,6 +1997,10 @@ movedb(const char *dbname, const char *tblspcname)
PG_ENSURE_ERROR_CLEANUP(movedb_failure_callback,
PointerGetDatum(&fparms));
{
Datum new_record[Natts_pg_database] = {0};
bool new_record_nulls[Natts_pg_database] = {0};
bool new_record_repl[Natts_pg_database] = {0};
/*
* Copy files from the old tablespace to the new one
*/
@@ -2042,10 +2040,6 @@ movedb(const char *dbname, const char *tblspcname)
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist", dbname)));
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
MemSet(new_record_repl, false, sizeof(new_record_repl));
new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_tblspcoid);
new_record_repl[Anum_pg_database_dattablespace - 1] = true;
@@ -2194,9 +2188,9 @@ AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel)
DefElem *dallowconnections = NULL;
DefElem *dconnlimit = NULL;
DefElem *dtablespace = NULL;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
bool new_record_repl[Natts_pg_database];
Datum new_record[Natts_pg_database] = {0};
bool new_record_nulls[Natts_pg_database] = {0};
bool new_record_repl[Natts_pg_database] = {0};
/* Extract options from the statement node tree */
foreach(option, stmt->options)
@@ -2305,10 +2299,6 @@ AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel)
/*
* Build an updated tuple, perusing the information just obtained
*/
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
MemSet(new_record_repl, false, sizeof(new_record_repl));
if (distemplate)
{
new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate);
@@ -2492,8 +2482,8 @@ AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
if (datForm->datdba != newOwnerId)
{
Datum repl_val[Natts_pg_database];
bool repl_null[Natts_pg_database];
bool repl_repl[Natts_pg_database];
bool repl_null[Natts_pg_database] = {0};
bool repl_repl[Natts_pg_database] = {0};
Acl *newAcl;
Datum aclDatum;
bool isNull;
@@ -2521,9 +2511,6 @@ AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied to change owner of database")));
memset(repl_null, false, sizeof(repl_null));
memset(repl_repl, false, sizeof(repl_repl));
repl_repl[Anum_pg_database_datdba - 1] = true;
repl_val[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(newOwnerId);

View File

@@ -1310,14 +1310,11 @@ pg_event_trigger_dropped_objects(PG_FUNCTION_ARGS)
{
SQLDropObject *obj;
int i = 0;
Datum values[12];
bool nulls[12];
Datum values[12] = {0};
bool nulls[12] = {0};
obj = slist_container(SQLDropObject, next, iter.cur);
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
/* classid */
values[i++] = ObjectIdGetDatum(obj->address.classId);
@@ -1840,7 +1837,7 @@ pg_event_trigger_ddl_commands(PG_FUNCTION_ARGS)
{
CollectedCommand *cmd = lfirst(lc);
Datum values[9];
bool nulls[9];
bool nulls[9] = {0};
ObjectAddress addr;
int i = 0;
@@ -1858,8 +1855,6 @@ pg_event_trigger_ddl_commands(PG_FUNCTION_ARGS)
!OidIsValid(cmd->d.simple.address.objectId))
continue;
MemSet(nulls, 0, sizeof(nulls));
switch (cmd->type)
{
case SCT_Simple:

View File

@@ -1806,8 +1806,8 @@ CreateTransform(CreateTransformStmt *stmt)
AclResult aclresult;
Form_pg_proc procstruct;
Datum values[Natts_pg_transform];
bool nulls[Natts_pg_transform];
bool replaces[Natts_pg_transform];
bool nulls[Natts_pg_transform] = {0};
bool replaces[Natts_pg_transform] = {0};
Oid transformid;
HeapTuple tuple;
HeapTuple newtuple;
@@ -1913,8 +1913,6 @@ CreateTransform(CreateTransformStmt *stmt)
values[Anum_pg_transform_trffromsql - 1] = ObjectIdGetDatum(fromsqlfuncid);
values[Anum_pg_transform_trftosql - 1] = ObjectIdGetDatum(tosqlfuncid);
MemSet(nulls, false, sizeof(nulls));
relation = table_open(TransformRelationId, RowExclusiveLock);
tuple = SearchSysCache2(TRFTYPELANG,
@@ -1931,7 +1929,6 @@ CreateTransform(CreateTransformStmt *stmt)
format_type_be(typeid),
stmt->lang)));
MemSet(replaces, false, sizeof(replaces));
replaces[Anum_pg_transform_trffromsql - 1] = true;
replaces[Anum_pg_transform_trftosql - 1] = true;

View File

@@ -685,12 +685,10 @@ pg_prepared_statement(PG_FUNCTION_ARGS)
{
TupleDesc result_desc;
Datum values[8];
bool nulls[8];
bool nulls[8] = {0};
result_desc = prep_stmt->plansource->resultDesc;
MemSet(nulls, 0, sizeof(nulls));
values[0] = CStringGetTextDatum(prep_stmt->stmt_name);
values[1] = CStringGetTextDatum(prep_stmt->plansource->query_string);
values[2] = TimestampTzGetDatum(prep_stmt->prepare_time);

View File

@@ -8988,15 +8988,15 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
bool recurse, bool recursing, LOCKMODE lockmode)
{
Relation pkrel;
int16 pkattnum[INDEX_MAX_KEYS];
int16 fkattnum[INDEX_MAX_KEYS];
Oid pktypoid[INDEX_MAX_KEYS];
Oid fktypoid[INDEX_MAX_KEYS];
Oid opclasses[INDEX_MAX_KEYS];
Oid pfeqoperators[INDEX_MAX_KEYS];
Oid ppeqoperators[INDEX_MAX_KEYS];
Oid ffeqoperators[INDEX_MAX_KEYS];
int16 fkdelsetcols[INDEX_MAX_KEYS];
int16 pkattnum[INDEX_MAX_KEYS] = {0};
int16 fkattnum[INDEX_MAX_KEYS] = {0};
Oid pktypoid[INDEX_MAX_KEYS] = {0};
Oid fktypoid[INDEX_MAX_KEYS] = {0};
Oid opclasses[INDEX_MAX_KEYS] = {0};
Oid pfeqoperators[INDEX_MAX_KEYS] = {0};
Oid ppeqoperators[INDEX_MAX_KEYS] = {0};
Oid ffeqoperators[INDEX_MAX_KEYS] = {0};
int16 fkdelsetcols[INDEX_MAX_KEYS] = {0};
int i;
int numfks,
numpks,
@@ -9088,16 +9088,6 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
* Look up the referencing attributes to make sure they exist, and record
* their attnums and type OIDs.
*/
MemSet(pkattnum, 0, sizeof(pkattnum));
MemSet(fkattnum, 0, sizeof(fkattnum));
MemSet(pktypoid, 0, sizeof(pktypoid));
MemSet(fktypoid, 0, sizeof(fktypoid));
MemSet(opclasses, 0, sizeof(opclasses));
MemSet(pfeqoperators, 0, sizeof(pfeqoperators));
MemSet(ppeqoperators, 0, sizeof(ppeqoperators));
MemSet(ffeqoperators, 0, sizeof(ffeqoperators));
MemSet(fkdelsetcols, 0, sizeof(fkdelsetcols));
numfks = transformColumnNameList(RelationGetRelid(rel),
fkconstraint->fk_attrs,
fkattnum, fktypoid);
@@ -11473,7 +11463,7 @@ validateForeignKeyConstraint(char *conname,
{
TupleTableSlot *slot;
TableScanDesc scan;
Trigger trig;
Trigger trig = {0};
Snapshot snapshot;
MemoryContext oldcxt;
MemoryContext perTupCxt;
@@ -11484,7 +11474,6 @@ validateForeignKeyConstraint(char *conname,
/*
* Build a trigger call structure; we'll need it either way.
*/
MemSet(&trig, 0, sizeof(trig));
trig.tgoid = InvalidOid;
trig.tgname = conname;
trig.tgenabled = TRIGGER_FIRES_ON_ORIGIN;
@@ -12758,15 +12747,11 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
int one = 1;
bool isNull;
Datum valuesAtt[Natts_pg_attribute];
bool nullsAtt[Natts_pg_attribute];
bool replacesAtt[Natts_pg_attribute];
Datum valuesAtt[Natts_pg_attribute] = {0};
bool nullsAtt[Natts_pg_attribute] = {0};
bool replacesAtt[Natts_pg_attribute] = {0};
HeapTuple newTup;
MemSet(valuesAtt, 0, sizeof(valuesAtt));
MemSet(nullsAtt, false, sizeof(nullsAtt));
MemSet(replacesAtt, false, sizeof(replacesAtt));
missingval = array_get_element(missingval,
1,
&one,
@@ -19192,7 +19177,7 @@ ATDetachCheckNoForeignKeyRefs(Relation partition)
HeapTuple tuple;
Form_pg_constraint constrForm;
Relation rel;
Trigger trig;
Trigger trig = {0};
tuple = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constrOid));
if (!HeapTupleIsValid(tuple))
@@ -19205,7 +19190,6 @@ ATDetachCheckNoForeignKeyRefs(Relation partition)
/* prevent data changes into the referencing table until commit */
rel = table_open(constrForm->conrelid, ShareLock);
MemSet(&trig, 0, sizeof(trig));
trig.tgoid = InvalidOid;
trig.tgname = NameStr(constrForm->conname);
trig.tgenabled = TRIGGER_FIRES_ON_ORIGIN;

View File

@@ -238,7 +238,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
#ifdef HAVE_SYMLINK
Relation rel;
Datum values[Natts_pg_tablespace];
bool nulls[Natts_pg_tablespace];
bool nulls[Natts_pg_tablespace] = {0};
HeapTuple tuple;
Oid tablespaceoid;
char *location;
@@ -340,8 +340,6 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
*/
rel = table_open(TableSpaceRelationId, RowExclusiveLock);
MemSet(nulls, false, sizeof(nulls));
if (IsBinaryUpgrade)
{
/* Use binary-upgrade override for tablespace oid */

View File

@@ -2570,9 +2570,9 @@ AlterDomainDefault(List *names, Node *defaultRaw)
Relation rel;
char *defaultValue;
Node *defaultExpr = NULL; /* NULL if no default specified */
Datum new_record[Natts_pg_type];
bool new_record_nulls[Natts_pg_type];
bool new_record_repl[Natts_pg_type];
Datum new_record[Natts_pg_type] = {0};
bool new_record_nulls[Natts_pg_type] = {0};
bool new_record_repl[Natts_pg_type] = {0};
HeapTuple newtuple;
Form_pg_type typTup;
ObjectAddress address;
@@ -2593,9 +2593,6 @@ AlterDomainDefault(List *names, Node *defaultRaw)
checkDomainOwner(tup);
/* Setup new tuple */
MemSet(new_record, (Datum) 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
MemSet(new_record_repl, false, sizeof(new_record_repl));
/* Store the new default into the tuple */
if (defaultRaw)

View File

@@ -74,8 +74,8 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
Relation pg_authid_rel;
TupleDesc pg_authid_dsc;
HeapTuple tuple;
Datum new_record[Natts_pg_authid];
bool new_record_nulls[Natts_pg_authid];
Datum new_record[Natts_pg_authid] = {0};
bool new_record_nulls[Natts_pg_authid] = {0};
Oid roleid;
ListCell *item;
ListCell *option;
@@ -338,12 +338,8 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
/*
* Build a tuple to insert
*/
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
new_record[Anum_pg_authid_rolname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->role));
new_record[Anum_pg_authid_rolsuper - 1] = BoolGetDatum(issuper);
new_record[Anum_pg_authid_rolinherit - 1] = BoolGetDatum(inherit);
new_record[Anum_pg_authid_rolcreaterole - 1] = BoolGetDatum(createrole);
@@ -492,9 +488,9 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
Oid
AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
{
Datum new_record[Natts_pg_authid];
bool new_record_nulls[Natts_pg_authid];
bool new_record_repl[Natts_pg_authid];
Datum new_record[Natts_pg_authid] = {0};
bool new_record_nulls[Natts_pg_authid] = {0};
bool new_record_repl[Natts_pg_authid] = {0};
Relation pg_authid_rel;
TupleDesc pg_authid_dsc;
HeapTuple tuple,
@@ -691,9 +687,6 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
/*
* Build an updated tuple, perusing the information just obtained
*/
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
MemSet(new_record_repl, false, sizeof(new_record_repl));
/*
* issuper/createrole/etc
@@ -1440,9 +1433,9 @@ AddRoleMems(const char *rolename, Oid roleid,
Oid memberid = lfirst_oid(iditem);
HeapTuple authmem_tuple;
HeapTuple tuple;
Datum new_record[Natts_pg_auth_members];
bool new_record_nulls[Natts_pg_auth_members];
bool new_record_repl[Natts_pg_auth_members];
Datum new_record[Natts_pg_auth_members] = {0};
bool new_record_nulls[Natts_pg_auth_members] = {0};
bool new_record_repl[Natts_pg_auth_members] = {0};
/*
* pg_database_owner is never a role member. Lifting this restriction
@@ -1500,10 +1493,6 @@ AddRoleMems(const char *rolename, Oid roleid,
}
/* Build a tuple to insert or update */
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
MemSet(new_record_repl, false, sizeof(new_record_repl));
new_record[Anum_pg_auth_members_roleid - 1] = ObjectIdGetDatum(roleid);
new_record[Anum_pg_auth_members_member - 1] = ObjectIdGetDatum(memberid);
new_record[Anum_pg_auth_members_grantor - 1] = ObjectIdGetDatum(grantorId);
@@ -1614,15 +1603,11 @@ DelRoleMems(const char *rolename, Oid roleid,
{
/* Just turn off the admin option */
HeapTuple tuple;
Datum new_record[Natts_pg_auth_members];
bool new_record_nulls[Natts_pg_auth_members];
bool new_record_repl[Natts_pg_auth_members];
Datum new_record[Natts_pg_auth_members] = {0};
bool new_record_nulls[Natts_pg_auth_members] = {0};
bool new_record_repl[Natts_pg_auth_members] = {0};
/* Build a tuple to update with */
MemSet(new_record, 0, sizeof(new_record));
MemSet(new_record_nulls, false, sizeof(new_record_nulls));
MemSet(new_record_repl, false, sizeof(new_record_repl));
new_record[Anum_pg_auth_members_admin_option - 1] = BoolGetDatum(false);
new_record_repl[Anum_pg_auth_members_admin_option - 1] = true;