From cc63360ce2e2584516b3bcc2dcdc9684d0330e13 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 2 Jan 2025 13:25:18 -0500
Subject: [PATCH] Some more Coverity paranoia.

This ones does require SSIZE_MAX... so let's see what the pipes say...
---
 src/xpdev/sockwrap.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/xpdev/sockwrap.c b/src/xpdev/sockwrap.c
index a31d1e5129..31e2949bfd 100644
--- a/src/xpdev/sockwrap.c
+++ b/src/xpdev/sockwrap.c
@@ -188,17 +188,20 @@ off_t sendfilesocket(int sock, int file, off_t *offset, off_t count)
 	}
 
 	while(total<count) {
-		rd=read(file,buf,sizeof(buf));
-		if(rd==-1)
+		rd = read(file, buf, sizeof(buf));
+		if (rd < 0)
 			return(-1);
-		if(rd==0)
+		if (rd == 0)
 			break;
-		for(i=wr=0;i<rd;i+=wr) {
-			wr=sendsocket(sock,buf+i,rd-i);
-			if(wr>0)
+		for (i = wr = 0; i < rd; i += wr) {
+			wr = sendsocket(sock,buf+i,rd-i);
+			if (wr > 0) {
+				if ((SSIZE_MAX - i) < wr)
+					wr = SSIZE_MAX - i;
 				continue;
-			if(wr==SOCKET_ERROR && SOCKET_ERRNO==EWOULDBLOCK) {
-				wr=0;
+			}
+			if (wr == SOCKET_ERROR && SOCKET_ERRNO == EWOULDBLOCK) {
+				wr = 0;
 				SLEEP(1);
 				continue;
 			}
-- 
GitLab