From 45fdb74dfb7ad61c8d8b24ccd96f762685fcda6c Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Wed, 31 May 2023 11:00:06 -0400 Subject: [PATCH] feat: added a safety for the while loop --- src/SonyBraviaDevice.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/SonyBraviaDevice.cs b/src/SonyBraviaDevice.cs index 80f7bba..015658a 100644 --- a/src/SonyBraviaDevice.cs +++ b/src/SonyBraviaDevice.cs @@ -580,8 +580,19 @@ private object ProcessRs232Response(object _) Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: bytes-'{0}' (len-'{1}') | buffer-'{2}' (len-'{3}')", bytes.ToReadableString(), bytes.Length, buffer.ToReadableString(), buffer.Length); - while (buffer.Length >= 3) + const int safety = 10; + var numberOfSpins = 0; + while (buffer.Length >= 3 && numberOfSpins <= safety) { + ++numberOfSpins; + if (numberOfSpins == safety) + Debug.Console(0, + this, + Debug.ErrorLogLevel.Notice, + "We hit our safety limit, something is wrong... Buffer:{0}, Bytes:{1}", + buffer.ToReadableString(), + bytes.ToReadableString()); + var message = buffer.GetFirstMessage(); Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: bytes-'{0}' (len-'{1}') | buffer-'{2}' (len-'{3}') | message-'{4}' (len-'{5}')", bytes.ToReadableString(), bytes.Length,