git: 89fe7b98fe45 - main - bhyvectl: do not return garbage from send_message

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Mon, 06 Mar 2023 13:05:24 UTC
The branch main has been updated by corvink:

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

commit 89fe7b98fe45cf56d60d0d4dfa1bfad3ba6908ec
Author:     Vitaliy Gusev <gusev.vitaliy@gmail.com>
AuthorDate: 2023-03-06 11:27:10 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-03-06 13:03:40 +0000

    bhyvectl: do not return garbage from send_message
    
    err is used uninitialized in some cases.
    
    Reviewed by:            corvink, markj
    MFC after:              1 week
    Sponsored by:           vStack
    Differential Revision:  https://reviews.freebsd.org/D38886
---
 usr.sbin/bhyvectl/bhyvectl.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
index cab1e6d72c56..5efccc085119 100644
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -1679,12 +1679,12 @@ static int
 send_message(const char *vmname, nvlist_t *nvl)
 {
 	struct sockaddr_un addr;
-	int err, socket_fd;
+	int err = 0, socket_fd;
 
 	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
 	if (socket_fd < 0) {
 		perror("Error creating bhyvectl socket");
-		err = -1;
+		err = errno;
 		goto done;
 	}
 
@@ -1700,8 +1700,10 @@ send_message(const char *vmname, nvlist_t *nvl)
 		goto done;
 	}
 
-	if (nvlist_send(socket_fd, nvl) < 0)
+	if (nvlist_send(socket_fd, nvl) < 0) {
 		perror("nvlist_send() failed");
+		err = errno;
+	}
 	nvlist_destroy(nvl);
 
 done: