git: 7f6245fd8e4e - main - pkg: Fix issues raised by clang-analyzer

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 19 Nov 2026 13:24:01 UTC
The branch main has been updated by des:

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

commit 7f6245fd8e4e1ec8c835d3e7e21b82774efe5c7b
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-19 13:23:25 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-19 13:23:46 +0000

    pkg: Fix issues raised by clang-analyzer
    
    MFC after:      1 week
    Reviewed by:    bapt
    Differential Revision:  https://reviews.freebsd.org/D58454
---
 usr.sbin/pkg/config.c | 16 +++++++++++-----
 usr.sbin/pkg/ecc.c    | 18 ++++++------------
 usr.sbin/pkg/pkg.c    | 11 ++++++-----
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/usr.sbin/pkg/config.c b/usr.sbin/pkg/config.c
index 8405a4c658fb..21535c813806 100644
--- a/usr.sbin/pkg/config.c
+++ b/usr.sbin/pkg/config.c
@@ -31,15 +31,16 @@
 #include <sys/utsname.h>
 #include <sys/sysctl.h>
 
+#include <assert.h>
+#include <ctype.h>
 #include <dirent.h>
-#include <ucl.h>
 #include <err.h>
 #include <errno.h>
 #include <libutil.h>
 #include <paths.h>
 #include <stdbool.h>
+#include <ucl.h>
 #include <unistd.h>
-#include <ctype.h>
 
 #include "config.h"
 
@@ -237,12 +238,17 @@ config_parse(const ucl_object_t *obj)
 			continue;
 		if (buf != NULL)
 			memset(buf, 0, bufsz);
+		errno = 0;
 		rewind(buffp);
+		assert(errno == 0); /* rewinding a memstream never fails */
 
-			for (j = 0; j < strlen(key); ++j)
-				fputc(toupper(key[j]), buffp);
-			fflush(buffp);
+		for (j = 0; key[j] != '\0'; ++j)
+			if (fputc(toupper(key[j]), buffp) == EOF)
+				err(1, NULL); /* failed to allocate */
+		if (fflush(buffp) != 0)
+			err(1, NULL); /* failed to allocate */
 
+		assert(buf != NULL);
 		for (i = 0; i < CONFIG_SIZE; i++) {
 			if (strcmp(buf, c[i].key) == 0)
 				break;
diff --git a/usr.sbin/pkg/ecc.c b/usr.sbin/pkg/ecc.c
index 01ce020bdba0..a975049e5739 100644
--- a/usr.sbin/pkg/ecc.c
+++ b/usr.sbin/pkg/ecc.c
@@ -306,7 +306,6 @@ ecc_extract_key_params(const uint8_t *oid, size_t oidlen,
 		if (oidlen != 1)
 			return (1);
 
-		ret = -1;
 		switch (*oid) {
 		case 0x0a:	/* secp256k1 */
 			ret = import_params(rawparams, &secp256k1_str_params);
@@ -318,12 +317,10 @@ ecc_extract_key_params(const uint8_t *oid, size_t oidlen,
 			ret = import_params(rawparams, &secp521r1_str_params);
 			break;
 		default:
-			return (1);
+			ret = -1;
 		}
 
-		if (ret == 0)
-			return (0);
-		return (1);
+		return (ret != 0);
 	}
 
 	if (oidlen >= sizeof(oid_brainpoolP) &&
@@ -334,7 +331,6 @@ ecc_extract_key_params(const uint8_t *oid, size_t oidlen,
 		if (oidlen != 1)
 			return (1);
 
-		ret = -1;
 		switch (*oid) {
 		case 0x07:	/* brainpoolP256r1 */
 			ret = import_params(rawparams, &brainpoolp256r1_str_params);
@@ -361,12 +357,10 @@ ecc_extract_key_params(const uint8_t *oid, size_t oidlen,
 			ret = import_params(rawparams, &brainpoolp512t1_str_params);
 			break;
 		default:
-			return (1);
+			ret = -1;
 		}
 
-		if (ret == 0)
-			return (0);
-		return (1);
+		return (ret != 0);
 	}
 
 #ifdef ECC_DEBUG
@@ -526,14 +520,14 @@ ecc_verify_data(const struct pkgsign_ctx *sctx,
 	int ret;
 	struct ecc_verify_cbdata cbdata;
 
-	ret = 1;
-
 	if (sigfile != NULL) {
 		cbdata.keyfp = fopen(sigfile, "r");
 		if (cbdata.keyfp == NULL) {
 			warn("fopen: %s", sigfile);
 			return (false);
 		}
+		cbdata.key = NULL;
+		cbdata.keylen = SIZE_MAX;
 	} else {
 		cbdata.keyfp = NULL;
 		cbdata.key = key;
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
index d6cb10797fb4..28f238d5a5e5 100644
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -653,6 +653,7 @@ parse_cert(int fd) {
 			fwrite(line, 1, linelen, tmpfp);
 	}
 
+	free(line);
 	fclose(fp);
 	fclose(sigfp);
 	fclose(certfp);
@@ -688,11 +689,11 @@ static bool
 verify_pubsignature(int fd_pkg, int fd_sig, struct repository *r)
 {
 	struct pubkey *pk;
-	char *data;
 	struct pkgsign_ctx *sctx;
+	const char *pubkey;
+	char *data;
 	size_t datasz;
 	bool ret;
-	const char *pubkey;
 
 	pk = NULL;
 	sctx = NULL;
@@ -743,8 +744,8 @@ verify_pubsignature(int fd_pkg, int fd_sig, struct repository *r)
 	}
 
 	/* Verify the signature. */
-	printf("Verifying signature with public key %s.a.. ", r->pubkey);
-	if (pkgsign_verify_data(sctx, data, datasz, r->pubkey, NULL, 0, pk->sig,
+	printf("Verifying signature with public key %s.a.. ", pubkey);
+	if (pkgsign_verify_data(sctx, data, datasz, pubkey, NULL, 0, pk->sig,
 	    pk->siglen) == false) {
 		fprintf(stderr, "Signature is not valid\n");
 		goto cleanup;
@@ -1381,5 +1382,5 @@ main(int argc, char *argv[])
 	execv(pkgpath, __DECONST(char **, pkgargv));
 
 	/* NOT REACHED */
-	return (EXIT_FAILURE);
+	exit(EXIT_FAILURE);
 }