git: c1557708f1fa - main - pkg: Fix Coverity warnings

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 16 Jan 2025 16:54:54 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=c1557708f1fae1bb9c8e23e3bbb2aa2b055e1211

commit c1557708f1fae1bb9c8e23e3bbb2aa2b055e1211
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-01-16 15:09:58 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-01-16 16:45:15 +0000

    pkg: Fix Coverity warnings
    
    - Fix allocation size in config_get_repositories().
    - Fix a memory leak in read_conf_file().
    - Avoid a null pointer dereference in an error path in
      verify_pubsignature().
    
    Fixes:  e3b4a51580fc ("pkg(7): expand VERSION_MAJOR, VERSION_MINOR, RELEASE and OSNAME")
    Fixes:  dc4581589a32 ("pkg: clean support for repositories")
---
 usr.sbin/pkg/config.c | 17 ++++++++++-------
 usr.sbin/pkg/pkg.c    |  2 +-
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/usr.sbin/pkg/config.c b/usr.sbin/pkg/config.c
index 26d7dd66b2a4..6649e75b7f6b 100644
--- a/usr.sbin/pkg/config.c
+++ b/usr.sbin/pkg/config.c
@@ -476,9 +476,9 @@ read_conf_file(const char *confpath, const char *requested_repo,
 {
 	struct ucl_parser *p;
 	ucl_object_t *obj = NULL;
-	const char *abi = pkg_get_myabi();
-	char *major, *minor;
+	char *abi = pkg_get_myabi(), *major, *minor;
 	struct utsname uts;
+	int ret;
 
 	if (uname(&uts))
 		err(EXIT_FAILURE, "uname");
@@ -502,9 +502,9 @@ read_conf_file(const char *confpath, const char *requested_repo,
 		if (errno != ENOENT)
 			errx(EXIT_FAILURE, "Unable to parse configuration "
 			    "file %s: %s", confpath, ucl_parser_get_error(p));
-		ucl_parser_free(p);
 		/* no configuration present */
-		return (1);
+		ret = 1;
+		goto out;
 	}
 
 	obj = ucl_parser_get_object(p);
@@ -517,13 +517,16 @@ read_conf_file(const char *confpath, const char *requested_repo,
 		else if (conftype == CONFFILE_REPO)
 			parse_repo_file(obj, requested_repo);
 	}
-
 	ucl_object_unref(obj);
+
+	ret = 0;
+out:
 	ucl_parser_free(p);
+	free(abi);
 	free(major);
 	free(minor);
 
-	return (0);
+	return (ret);
 }
 
 static void
@@ -674,7 +677,7 @@ config_get_repositories(void)
 {
 	if (STAILQ_EMPTY(&repositories)) {
 		/* Fall back to PACKAGESITE - deprecated - */
-		struct repository *r = calloc(1, sizeof(r));
+		struct repository *r = calloc(1, sizeof(*r));
 		if (r == NULL)
 			err(EXIT_FAILURE, "calloc");
 		r->name = strdup("fallback");
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
index 21ee1cd4bd30..92fdbf0ebff8 100644
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -664,7 +664,7 @@ verify_pubsignature(int fd_pkg, int fd_sig, struct repository *r)
 		pubkey = r->pubkey;
 	} else {
 		if (config_string(PUBKEY, &pubkey) != 0) {
-			warnx("No CONFIG_PUBKEY defined for %s", r->name);
+			warnx("No CONFIG_PUBKEY defined");
 			goto cleanup;
 		}
 	}