[pacman-dev] [PATCH 2/2] Use c99 struct initialization to avoid memset calls

Dave Reisner dreisner at archlinux.org
Tue Dec 24 23:17:48 UTC 2019


This is guaranteed less error prone than calling memset and hoping the
human gets the argument order correct.
---
 lib/libalpm/be_local.c   |  5 +----
 lib/libalpm/be_package.c |  7 ++-----
 lib/libalpm/be_sync.c    |  7 ++-----
 lib/libalpm/dload.c      | 13 ++++++-------
 lib/libalpm/signing.c    | 23 +++++++----------------
 lib/libalpm/util.c       |  2 +-
 src/pacman/conf.c        |  3 +--
 7 files changed, 20 insertions(+), 40 deletions(-)

diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c
index b89acf05..78e32a24 100644
--- a/lib/libalpm/be_local.c
+++ b/lib/libalpm/be_local.c
@@ -694,7 +694,7 @@ char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
 static int local_db_read(alpm_pkg_t *info, int inforeq)
 {
 	FILE *fp = NULL;
-	char line[1024];
+	char line[1024] = {0};
 	alpm_db_t *db = info->origin_data.db;
 
 	/* bitmask logic here:
@@ -717,9 +717,6 @@ static int local_db_read(alpm_pkg_t *info, int inforeq)
 			"loading package data for %s : level=0x%x\n",
 			info->name, inforeq);
 
-	/* clear out 'line', to be certain - and to make valgrind happy */
-	memset(line, 0, sizeof(line));
-
 	/* DESC */
 	if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
 		char *path = _alpm_local_db_pkgpath(db, info, "desc");
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 5ffea875..9a8b4410 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -164,9 +164,8 @@ static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t *
 	char *ptr = NULL;
 	char *key = NULL;
 	int ret, linenum = 0;
-	struct archive_read_buffer buf;
+	struct archive_read_buffer buf = {0};
 
-	memset(&buf, 0, sizeof(buf));
 	/* 512K for a line length seems reasonable */
 	buf.max_line_size = 512 * 1024;
 
@@ -448,13 +447,11 @@ static int build_filelist_from_mtree(alpm_handle_t *handle, alpm_pkg_t *pkg, str
 	char *mtree_data = NULL;
 	struct archive *mtree;
 	struct archive_entry *mtree_entry = NULL;
-	alpm_filelist_t filelist;
+	alpm_filelist_t filelist = {0};
 
 	_alpm_log(handle, ALPM_LOG_DEBUG,
 			"found mtree for package %s, getting file list\n", pkg->filename);
 
-	memset(&filelist, 0, sizeof(alpm_filelist_t));
-
 	/* create a new archive to parse the mtree and load it from archive into memory */
 	/* TODO: split this into a function */
 	if((mtree = archive_read_new()) == NULL) {
diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c
index 2c76fe83..4614610c 100644
--- a/lib/libalpm/be_sync.c
+++ b/lib/libalpm/be_sync.c
@@ -219,12 +219,10 @@ int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
 
 	for(i = db->servers; i; i = i->next) {
 		const char *server = i->data, *final_db_url = NULL;
-		struct dload_payload payload;
+		struct dload_payload payload = {};
 		size_t len;
 		int sig_ret = 0;
 
-		memset(&payload, 0, sizeof(struct dload_payload));
-
 		/* set hard upper limit of 25MiB */
 		payload.max_size = 25 * 1024 * 1024;
 
@@ -601,7 +599,7 @@ static int sync_db_read(alpm_db_t *db, struct archive *archive,
 {
 	const char *entryname, *filename;
 	alpm_pkg_t *pkg;
-	struct archive_read_buffer buf;
+	struct archive_read_buffer buf = {0};
 
 	entryname = archive_entry_pathname(entry);
 	if(entryname == NULL) {
@@ -613,7 +611,6 @@ static int sync_db_read(alpm_db_t *db, struct archive *archive,
 	_alpm_log(db->handle, ALPM_LOG_FUNCTION, "loading package data from archive entry %s\n",
 			entryname);
 
-	memset(&buf, 0, sizeof(buf));
 	/* 512K for a line length seems reasonable */
 	buf.max_line_size = 512 * 1024;
 
diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index 40a1d07d..b3e6a411 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -195,9 +195,10 @@ static int curl_gethost(const char *url, char *buffer, size_t buf_len)
 static int utimes_long(const char *path, long seconds)
 {
 	if(seconds != -1) {
-		struct timeval tv[2];
-		memset(&tv, 0, sizeof(tv));
-		tv[0].tv_sec = tv[1].tv_sec = seconds;
+		struct timeval tv[2] = {
+			{ .tv_sec = seconds, },
+			{ .tv_sec = seconds, },
+		};
 		return utimes(path, tv);
 	}
 	return 0;
@@ -657,7 +658,7 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
 	char *filepath;
 	const char *cachedir, *final_pkg_url = NULL;
 	char *final_file = NULL;
-	struct dload_payload payload;
+	struct dload_payload payload = {0};
 	int ret = 0;
 
 	CHECK_HANDLE(handle, return NULL);
@@ -666,8 +667,6 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
 	/* find a valid cache dir to download to */
 	cachedir = _alpm_filecache_setup(handle);
 
-	memset(&payload, 0, sizeof(struct dload_payload));
-
 	/* attempt to find the file in our pkgcache */
 	filepath = filecache_find_url(handle, url);
 	if(filepath == NULL) {
@@ -740,7 +739,7 @@ void _alpm_dload_payload_reset(struct dload_payload *payload)
 	FREE(payload->destfile_name);
 	FREE(payload->content_disp_name);
 	FREE(payload->fileurl);
-	memset(payload, '\0', sizeof(*payload));
+	*payload = (struct dload_payload){0};
 }
 
 void _alpm_dload_payload_reset_for_retry(struct dload_payload *payload)
diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c
index 3c4f883f..257a287f 100644
--- a/lib/libalpm/signing.c
+++ b/lib/libalpm/signing.c
@@ -217,7 +217,7 @@ gpg_error:
 int _alpm_key_in_keychain(alpm_handle_t *handle, const char *fpr)
 {
 	gpgme_error_t gpg_err;
-	gpgme_ctx_t ctx;
+	gpgme_ctx_t ctx = {0};
 	gpgme_key_t key;
 	int ret = -1;
 
@@ -231,7 +231,6 @@ int _alpm_key_in_keychain(alpm_handle_t *handle, const char *fpr)
 		goto error;
 	}
 
-	memset(&ctx, 0, sizeof(ctx));
 	gpg_err = gpgme_new(&ctx);
 	CHECK_ERR();
 
@@ -267,12 +266,11 @@ error:
 static int key_import_wkd(alpm_handle_t *handle, const char *email)
 {
 	gpgme_error_t gpg_err;
-	gpgme_ctx_t ctx;
+	gpgme_ctx_t ctx = {0};
 	gpgme_keylist_mode_t mode;
 	gpgme_key_t key;
 	int ret = -1;
 
-	memset(&ctx, 0, sizeof(ctx));
 	gpg_err = gpgme_new(&ctx);
 	CHECK_ERR();
 
@@ -309,7 +307,7 @@ static int key_search_keyserver(alpm_handle_t *handle, const char *fpr,
 		alpm_pgpkey_t *pgpkey)
 {
 	gpgme_error_t gpg_err;
-	gpgme_ctx_t ctx;
+	gpgme_ctx_t ctx = {0};
 	gpgme_keylist_mode_t mode;
 	gpgme_key_t key;
 	int ret = -1;
@@ -322,7 +320,6 @@ static int key_search_keyserver(alpm_handle_t *handle, const char *fpr,
 	MALLOC(full_fpr, fpr_len + 3, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
 	sprintf(full_fpr, "0x%s", fpr);
 
-	memset(&ctx, 0, sizeof(ctx));
 	gpg_err = gpgme_new(&ctx);
 	CHECK_ERR();
 
@@ -431,7 +428,7 @@ gpg_error:
 static int key_import_keyserver(alpm_handle_t *handle, alpm_pgpkey_t *key)
 {
 	gpgme_error_t gpg_err;
-	gpgme_ctx_t ctx;
+	gpgme_ctx_t ctx = {0};
 	gpgme_key_t keys[2];
 	gpgme_import_result_t result;
 	int ret = -1;
@@ -442,7 +439,6 @@ static int key_import_keyserver(alpm_handle_t *handle, alpm_pgpkey_t *key)
 		return -1;
 	}
 
-	memset(&ctx, 0, sizeof(ctx));
 	gpg_err = gpgme_new(&ctx);
 	CHECK_ERR();
 
@@ -507,7 +503,7 @@ static int email_from_uid(const char *uid, char **email)
 int _alpm_key_import(alpm_handle_t *handle, const char *uid, const char *fpr)
 {
 	int ret = -1;
-	alpm_pgpkey_t fetch_key;
+	alpm_pgpkey_t fetch_key = {0};
 	char *email;
 
 	if(_alpm_access(handle, handle->gpgdir, "pubring.gpg", W_OK)) {
@@ -516,7 +512,6 @@ int _alpm_key_import(alpm_handle_t *handle, const char *uid, const char *fpr)
 		return -1;
 	}
 
-	memset(&fetch_key, 0, sizeof(fetch_key));
 	STRDUP(fetch_key.uid, uid, return -1);
 	STRDUP(fetch_key.fingerprint, fpr, return -1);
 
@@ -576,8 +571,8 @@ int _alpm_gpgme_checksig(alpm_handle_t *handle, const char *path,
 {
 	int ret = -1, sigcount;
 	gpgme_error_t gpg_err = 0;
-	gpgme_ctx_t ctx;
-	gpgme_data_t filedata, sigdata;
+	gpgme_ctx_t ctx = {0};
+	gpgme_data_t filedata = {0}, sigdata = {0};
 	gpgme_verify_result_t verify_result;
 	gpgme_signature_t gpgsig;
 	char *sigpath = NULL;
@@ -618,10 +613,6 @@ int _alpm_gpgme_checksig(alpm_handle_t *handle, const char *path,
 
 	_alpm_log(handle, ALPM_LOG_DEBUG, "checking signature for %s\n", path);
 
-	memset(&ctx, 0, sizeof(ctx));
-	memset(&sigdata, 0, sizeof(sigdata));
-	memset(&filedata, 0, sizeof(filedata));
-
 	gpg_err = gpgme_new(&ctx);
 	CHECK_ERR();
 
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index a4d62c7c..d12a4403 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -1191,7 +1191,7 @@ cleanup:
 	{
 		int ret = b->ret;
 		FREE(b->line);
-		memset(b, 0, sizeof(struct archive_read_buffer));
+		*b = (struct archive_read_buffer){0};
 		return ret;
 	}
 }
diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index 3b475151..fe6d6971 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -1117,8 +1117,7 @@ int setdefaults(config_t *c)
 
 int parseconfigfile(const char *file)
 {
-	struct section_t section;
-	memset(&section, 0, sizeof(struct section_t));
+	struct section_t section = {0};
 	pm_printf(ALPM_LOG_DEBUG, "config: attempting to read file %s\n", file);
 	return parse_ini(file, _parse_directive, &section);
 }
-- 
2.24.1


More information about the pacman-dev mailing list