git: c9faf6987406 - main - bhyve: Fix some warnings in the snapshot code

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Sun, 23 Oct 2022 15:18:41 UTC
The branch main has been updated by markj:

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

commit c9faf6987406781debb06ec5e40aa1de6d11ac4f
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-10-22 17:41:33 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-10-23 15:11:33 +0000

    bhyve: Fix some warnings in the snapshot code
    
    - Qualify unexported symbols with "static".
    - Drop some unnecessary and incorrect casts.
    - Avoid arithmetic on void pointers.
    - Avoid signed/unsigned comparisons in loops which use nitems() as a
      bound.
    
    No functional change intended.
    
    MFC after:      1 week
---
 usr.sbin/bhyve/pci_emul.c |  2 +-
 usr.sbin/bhyve/snapshot.c | 35 ++++++++++++++++++-----------------
 2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/usr.sbin/bhyve/pci_emul.c b/usr.sbin/bhyve/pci_emul.c
index 2087c6597852..6e584d0b6626 100644
--- a/usr.sbin/bhyve/pci_emul.c
+++ b/usr.sbin/bhyve/pci_emul.c
@@ -2622,7 +2622,7 @@ pci_emul_dior(struct vmctx *ctx __unused, int vcpu __unused,
 }
 
 #ifdef BHYVE_SNAPSHOT
-int
+static int
 pci_emul_snapshot(struct vm_snapshot_meta *meta __unused)
 {
 	return (0);
diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c
index 881c91973a02..7ec923d6faec 100644
--- a/usr.sbin/bhyve/snapshot.c
+++ b/usr.sbin/bhyve/snapshot.c
@@ -138,7 +138,7 @@ static sig_t old_winch_handler;
  _a < _b ? _a : _b;       	\
  })
 
-const struct vm_snapshot_dev_info snapshot_devs[] = {
+static const struct vm_snapshot_dev_info snapshot_devs[] = {
 	{ "atkbdc",	atkbdc_snapshot,	NULL,		NULL		},
 	{ "virtio-net",	pci_snapshot,		pci_pause,	pci_resume	},
 	{ "virtio-blk",	pci_snapshot,		pci_pause,	pci_resume	},
@@ -152,7 +152,7 @@ const struct vm_snapshot_dev_info snapshot_devs[] = {
 	{ "ahci-cd",	pci_snapshot,		pci_pause,	pci_resume	},
 };
 
-const struct vm_snapshot_kern_info snapshot_kern_structs[] = {
+static const struct vm_snapshot_kern_info snapshot_kern_structs[] = {
 	{ "vhpet",	STRUCT_VHPET	},
 	{ "vm",		STRUCT_VM	},
 	{ "vmx",	STRUCT_VMX	},
@@ -298,13 +298,14 @@ err_load_kdata:
 static int
 load_metadata_file(const char *filename, struct restore_state *rstate)
 {
-	const ucl_object_t *obj;
+	ucl_object_t *obj;
 	struct ucl_parser *parser;
 	int err;
 
 	parser = ucl_parser_new(UCL_PARSER_DEFAULT);
 	if (parser == NULL) {
 		fprintf(stderr, "Failed to initialize UCL parser.\n");
+		err = -1;
 		goto err_load_metadata;
 	}
 
@@ -430,7 +431,7 @@ lookup_struct(enum snapshot_req struct_id, struct restore_state *rstate,
 		return (NULL);
 	}
 
-	if (ucl_object_type((ucl_object_t *)structs) != UCL_ARRAY) {
+	if (ucl_object_type(structs) != UCL_ARRAY) {
 		fprintf(stderr, "Object '%s' is not an array.\n",
 		JSON_STRUCT_ARR_KEY);
 		return (NULL);
@@ -502,7 +503,7 @@ lookup_dev(const char *dev_name, struct restore_state *rstate,
 		return (NULL);
 	}
 
-	if (ucl_object_type((ucl_object_t *)devs) != UCL_ARRAY) {
+	if (ucl_object_type(devs) != UCL_ARRAY) {
 		fprintf(stderr, "Object '%s' is not an array.\n",
 			JSON_DEV_ARR_KEY);
 		return (NULL);
@@ -530,7 +531,7 @@ lookup_basic_metadata_object(struct restore_state *rstate)
 		return (NULL);
 	}
 
-	if (ucl_object_type((ucl_object_t *)basic_meta_obj) != UCL_OBJECT) {
+	if (ucl_object_type(basic_meta_obj) != UCL_OBJECT) {
 		fprintf(stderr, "Object '%s' is not a JSON object.\n",
 		JSON_BASIC_METADATA_KEY);
 		return (NULL);
@@ -620,7 +621,7 @@ print_progress(size_t crtval, const size_t maxval)
 	static const size_t len = sizeof(prog_buf);
 
 	static size_t div;
-	static char *div_str;
+	static const char *div_str;
 
 	static char wip_bar[] = { '/', '-', '\\', '|' };
 	static int wip_idx = 0;
@@ -777,7 +778,7 @@ vm_snapshot_mem_part(const int snapfd, const size_t foff, void *src,
 			return (-1);
 		}
 
-		src += done;
+		src = (uint8_t *)src + done;
 		part_done += done;
 		rem -= done;
 	}
@@ -912,8 +913,8 @@ done:
 int
 vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate)
 {
+	size_t i;
 	int ret;
-	int i;
 
 	for (i = 0; i < nitems(snapshot_kern_structs); i++) {
 		ret = vm_restore_kern_struct(ctx, rstate,
@@ -925,7 +926,7 @@ vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate)
 	return (0);
 }
 
-int
+static int
 vm_restore_user_dev(struct vmctx *ctx, struct restore_state *rstate,
 		    const struct vm_snapshot_dev_info *info)
 {
@@ -975,8 +976,8 @@ vm_restore_user_dev(struct vmctx *ctx, struct restore_state *rstate,
 int
 vm_restore_user_devs(struct vmctx *ctx, struct restore_state *rstate)
 {
+	size_t i;
 	int ret;
-	int i;
 
 	for (i = 0; i < nitems(snapshot_devs); i++) {
 		ret = vm_restore_user_dev(ctx, rstate, &snapshot_devs[i]);
@@ -991,8 +992,8 @@ int
 vm_pause_user_devs(struct vmctx *ctx)
 {
 	const struct vm_snapshot_dev_info *info;
+	size_t i;
 	int ret;
-	int i;
 
 	for (i = 0; i < nitems(snapshot_devs); i++) {
 		info = &snapshot_devs[i];
@@ -1011,8 +1012,8 @@ int
 vm_resume_user_devs(struct vmctx *ctx)
 {
 	const struct vm_snapshot_dev_info *info;
+	size_t i;
 	int ret;
-	int i;
 
 	for (i = 0; i < nitems(snapshot_devs); i++) {
 		info = &snapshot_devs[i];
@@ -1070,8 +1071,8 @@ done:
 static int
 vm_snapshot_kern_structs(struct vmctx *ctx, int data_fd, xo_handle_t *xop)
 {
-	int ret, i, error;
-	size_t offset, buf_size;
+	int ret, error;
+	size_t buf_size, i, offset;
 	char *buffer;
 	struct vm_snapshot_meta *meta;
 
@@ -1185,10 +1186,10 @@ vm_snapshot_user_dev(const struct vm_snapshot_dev_info *info,
 static int
 vm_snapshot_user_devs(struct vmctx *ctx, int data_fd, xo_handle_t *xop)
 {
-	int ret, i;
+	int ret;
 	off_t offset;
 	void *buffer;
-	size_t buf_size;
+	size_t buf_size, i;
 	struct vm_snapshot_meta *meta;
 
 	buf_size = SNAPSHOT_BUFFER_SIZE;