Tcl Source Code

Artifact [8b2c99c165]
Login

Artifact 8b2c99c16517b67db88d65d3de6e4ee4404c3abaa6917141737750774f98e70e:

Attachment "0001-unix-Fix-encoding-system-in-ISO-8859-1-locales.patch" to ticket [5fca83d78c] added by bhaible 2024-07-01 18:35:40. (unpublished)
From 0852b57897a5467af4f4b70688fa2caccac6296f Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Mon, 1 Jul 2024 20:26:50 +0200
Subject: [PATCH] unix: Fix [encoding system] in ISO-8859-1 locales

* unix/tclUnixInit.c (SearchKnownEncodings): Fix logic bug in binary search.
---
 unix/tclUnixInit.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/unix/tclUnixInit.c b/unix/tclUnixInit.c
index 63167c6c68..9f19075682 100644
--- a/unix/tclUnixInit.c
+++ b/unix/tclUnixInit.c
@@ -597,17 +597,21 @@ SearchKnownEncodings(
     int left = 0;
     int right = sizeof(localeTable)/sizeof(LocaleTable);
 
+    /* Here, search for i in the interval left <= i < right. */
     while (left < right) {
 	int test = (left + right)/2;
 	int code = strcmp(localeTable[test].lang, encoding);
 
 	if (code == 0) {
+	    /* Found it at i == test.  */
 	    return localeTable[test].encoding;
 	}
 	if (code < 0) {
+	    /* Restrict the search to the interval test < i < right. */
 	    left = test+1;
 	} else {
-	    right = test-1;
+	    /* Restrict the search to the interval left <= i < test. */
+	    right = test;
 	}
     }
     return NULL;
-- 
2.34.1