git: 46f5c828961e - main - bhyve: Address warnings in blockif_proc()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 25 Oct 2022 15:17:07 UTC
The branch main has been updated by markj:

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

commit 46f5c828961e64646c46ff9e3bc8e55f46f3e7bb
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-10-25 13:16:23 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-10-25 15:16:56 +0000

    bhyve: Address warnings in blockif_proc()
    
    - Use unsigned types for all arithmetic.  Use a new signed variable for
      holding the return value of pread() and pwrite().
    - Handle short I/O from pwrite().
    
    MFC after:      1 week
---
 usr.sbin/bhyve/block_if.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/usr.sbin/bhyve/block_if.c b/usr.sbin/bhyve/block_if.c
index b22ca140a80e..c8454a679bcd 100644
--- a/usr.sbin/bhyve/block_if.c
+++ b/usr.sbin/bhyve/block_if.c
@@ -233,35 +233,39 @@ blockif_flush_bc(struct blockif_ctxt *bc)
 static void
 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf)
 {
+	struct spacectl_range range;
 	struct blockif_req *br;
 	off_t arg[2];
-	ssize_t clen, len, off, boff, voff;
+	ssize_t n;
+	size_t clen, len, off, boff, voff;
 	int i, err;
-	struct spacectl_range range;
 
 	br = be->be_req;
+	assert(br->br_resid >= 0);
+
 	if (br->br_iovcnt <= 1)
 		buf = NULL;
 	err = 0;
 	switch (be->be_op) {
 	case BOP_READ:
 		if (buf == NULL) {
-			if ((len = preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
-				   br->br_offset)) < 0)
+			if ((n = preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
+			    br->br_offset)) < 0)
 				err = errno;
 			else
-				br->br_resid -= len;
+				br->br_resid -= n;
 			break;
 		}
 		i = 0;
 		off = voff = 0;
 		while (br->br_resid > 0) {
 			len = MIN(br->br_resid, MAXPHYS);
-			if (pread(bc->bc_fd, buf, len, br->br_offset +
-			    off) < 0) {
+			n = pread(bc->bc_fd, buf, len, br->br_offset + off);
+			if (n < 0) {
 				err = errno;
 				break;
 			}
+			len = (size_t)n;
 			boff = 0;
 			do {
 				clen = MIN(len - boff, br->br_iov[i].iov_len -
@@ -286,11 +290,11 @@ blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf)
 			break;
 		}
 		if (buf == NULL) {
-			if ((len = pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
-				    br->br_offset)) < 0)
+			if ((n = pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
+			    br->br_offset)) < 0)
 				err = errno;
 			else
-				br->br_resid -= len;
+				br->br_resid -= n;
 			break;
 		}
 		i = 0;
@@ -312,13 +316,14 @@ blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf)
 				}
 				boff += clen;
 			} while (boff < len);
-			if (pwrite(bc->bc_fd, buf, len, br->br_offset +
-			    off) < 0) {
+
+			n = pwrite(bc->bc_fd, buf, len, br->br_offset + off);
+			if (n < 0) {
 				err = errno;
 				break;
 			}
-			off += len;
-			br->br_resid -= len;
+			off += n;
+			br->br_resid -= n;
 		}
 		break;
 	case BOP_FLUSH: