From 70afe70e0e84ceb715d760b2d43444ae120155f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Mon, 24 Jan 2022 19:09:22 -0500
Subject: [PATCH] Add new definition for infinite timeout, and use that
 explicitly

The value of the macro is INT_MIN, not zero to protect agains weird
edge cases.

I considered INT_MAX, but figured you could get there in sane ways.
---
 src/comio/comio.c     | 2 +-
 src/comio/comio.h     | 2 ++
 src/sexpots/sexpots.c | 5 ++++-
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/comio/comio.c b/src/comio/comio.c
index 63abbef1ca..ef901e2c9b 100644
--- a/src/comio/comio.c
+++ b/src/comio/comio.c
@@ -30,7 +30,7 @@ size_t comReadBuf(COM_HANDLE handle, char* buf, size_t buflen, const char* termi
 
 	while(len < buflen) {
 		if(!comReadByte(handle, &ch)) {
-			if(timeout > 0 && msclock()-start >= timeout)
+			if(timeout != COM_INFINITE_TIMEOUT && msclock()-start >= timeout)
 				break;
 			YIELD();
 			continue;
diff --git a/src/comio/comio.h b/src/comio/comio.h
index 557381ba28..09cfc860cb 100644
--- a/src/comio/comio.h
+++ b/src/comio/comio.h
@@ -38,9 +38,11 @@
 #ifndef _COMIO_H
 #define _COMIO_H
 
+#include <limits.h>	/* INT_MAX */
 #include <gen_defs.h>	/* BOOL */
 
 #define COM_ERROR						-1
+#define COM_INFINITE_TIMEOUT	INT_MIN
 
 #ifdef _WIN32
 	#if defined(COMIO_IMPORTS) || defined(COMIO_EXPORTS)
diff --git a/src/sexpots/sexpots.c b/src/sexpots/sexpots.c
index 8feaa2d382..1d5a440619 100644
--- a/src/sexpots/sexpots.c
+++ b/src/sexpots/sexpots.c
@@ -1592,10 +1592,13 @@ service_loop(int argc, char** argv)
 		comWriteString(com_handle, banner);
 		comWriteString(com_handle, "\r\n");
 		if(prompt[0] != '\0') {
+			int ptimeout = prompt_timeout * 1000;
+			if (ptimeout == 0)
+				ptimeout = COM_INFINITE_TIMEOUT;
 			parse_tcp_section("TCP");
 			comWriteString(com_handle, prompt);
 			char ch;
-			if(comReadBuf(com_handle, &ch, sizeof(ch), NULL, prompt_timeout * 1000)) {
+			if(comReadBuf(com_handle, &ch, sizeof(ch), NULL, ptimeout)) {
 				if(IS_CONTROL(ch))
 					lprintf(LOG_WARNING, "Received a control character (%d) in response to prompt", ch);
 				else {
-- 
GitLab