From 10d8683f81ae5dd601ace12c7617de7ae44ed698 Mon Sep 17 00:00:00 2001
From: Eric Oulashin <nightfox@synchro.net>
Date: Sat, 28 Oct 2023 06:40:48 +0000
Subject: [PATCH] dd_lightbar_menu.js: Length off-by-1 fix in
 substrWithAttrCodes()

---
 exec/load/dd_lightbar_menu.js | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/exec/load/dd_lightbar_menu.js b/exec/load/dd_lightbar_menu.js
index 275dc38588..dae0171c9e 100644
--- a/exec/load/dd_lightbar_menu.js
+++ b/exec/load/dd_lightbar_menu.js
@@ -3572,7 +3572,12 @@ function substrWithAttrCodes(pStr, pStartIdx, pLen)
 	// Find the actual start & end indexes, considering (not counting) attribute codes,
 	// and return the substring including any applicable attributes from the string
 	var actualStartIdx = findIdxConsideringAttrs(pStr, startIdx);
-	var actualEndIdx = findIdxConsideringAttrs(pStr, startIdx+len+1) + 1; // Initially tried just startIdx+len without the +1
+	var actualEndIdx = findIdxConsideringAttrs(pStr, startIdx+len+1);
+	// With the actual start & end indexes, make sure we'll get the string
+	// length desired; if not, adjust actualEndIdx;
+	var lenWithActualIndexes = actualEndIdx - actualStartIdx;
+	if (actualEndIdx-actualStartIdx < len)
+		actualEndIdx += len - lenWithActualIndexes;
 	return getAttrsBeforeStrIdx(pStr, actualStartIdx) + pStr.substring(actualStartIdx, actualEndIdx);
 }
 // Helper for substrWithAttrCodes(): Maps a 'visual' character index in a string to its
@@ -3926,4 +3931,4 @@ function logStackTrace(levels) {
     else {
         console.print(callstack.join("\r\n"));
     }
-}
\ No newline at end of file
+}
-- 
GitLab