From 1df8faa5407e66c76b1105472a7fe9a970e79a4b Mon Sep 17 00:00:00 2001 From: Frank Morgner Date: Mon, 9 Sep 2024 15:58:34 +0200 Subject: [PATCH] fixed possible integer underflow fixes CID 443646, thanks coverety scan (cherry picked from commit 007230711693cf12ae59a8439f22399f9727cb24) --- virtualsmartcard/src/vpcd/vpcd.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/virtualsmartcard/src/vpcd/vpcd.c b/virtualsmartcard/src/vpcd/vpcd.c index 882feb1..60e5891 100644 --- a/virtualsmartcard/src/vpcd/vpcd.c +++ b/virtualsmartcard/src/vpcd/vpcd.c @@ -64,21 +64,23 @@ static SOCKET connectsock(const char *hostname, unsigned short port); ssize_t sendall(SOCKET sock, const void *buffer, size_t size) { - size_t sent; + size_t sent = 0; ssize_t r; /* FIXME we should actually check the length instead of simply casting from * size_t to ssize_t (or int), which have both the same width! */ - for (sent = 0; sent < size; sent += r) { + while (sent < size) { r = send(sock, (void *) (((unsigned char *) buffer)+sent), #ifdef _WIN32 (int) #endif (size-sent), MSG_NOSIGNAL); - if (r < 0) - return r; - } + if (r < 0) + return r; + + sent += r; + } return (ssize_t) sent; }