This patch addresses #1823, which was caused by an off-by-one error
in the check of the end-index, which is exclusive. Thanks to "siiky" for
reporting this.


felix

From 8e29a4332873ea4cbb98c8847bd5648033b79ae9 Mon Sep 17 00:00:00 2001
From: felix <fe...@call-with-current-continuation.org>
Date: Mon, 15 Apr 2024 12:48:23 +0200
Subject: [PATCH] =?UTF-8?q?Fixed=20off-by-one-errors=20in=20end-limit=20of?=
 =?UTF-8?q?=20substring[-ci]=3D=3F?=

---
 data-structures.scm             | 4 ++--
 tests/data-structures-tests.scm | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/data-structures.scm b/data-structures.scm
index 642d2a59..ad09d26b 100644
--- a/data-structures.scm
+++ b/data-structures.scm
@@ -160,7 +160,7 @@
   (let* ((maxlen (fxmin (fx- (##sys#size s1) start1)
                         (fx- (##sys#size s2) start2)))
          (len (if n
-                  (begin (##sys#check-range n 0 maxlen 'substring=?) n)
+                  (begin (##sys#check-range n 0 (fx+ maxlen 1) 'substring=?) n)
                   maxlen)))
     (##core#inline "C_substring_compare" s1 s2 start1 start2 len) ) )
 
@@ -175,7 +175,7 @@
   (let* ((maxlen (fxmin (fx- (##sys#size s1) start1)
                         (fx- (##sys#size s2) start2)))
          (len (if n
-                  (begin (##sys#check-range n 0 maxlen 'substring-ci=?) n)
+                  (begin (##sys#check-range n 0 (fx+ maxlen 1) 
'substring-ci=?) n)
                   maxlen)))
     (##core#inline "C_substring_compare_case_insensitive"
                   s1 s2 start1 start2 len) ) )
diff --git a/tests/data-structures-tests.scm b/tests/data-structures-tests.scm
index 17a3dd58..eb779531 100644
--- a/tests/data-structures-tests.scm
+++ b/tests/data-structures-tests.scm
@@ -46,6 +46,8 @@
 (assert (= 2 (substring-index-ci "o\x00bar" "foo\x00BAR")))
 (assert (not (substring=? "foo\x00a" "foo\x00b" 1 1)))
 (assert (not (substring-ci=? "foo\x00a" "foo\x00b" 1 1)))
+(assert (substring=? "foo" "foo" 0 0 3))
+(assert (substring-ci=? "foo" "foo" 0 0 3))
 (assert (not (substring-index "o\x00bar" "foo\x00baz")))
 (assert (not (substring-index-ci "o\x00bar" "foo\x00baz")))
 (assert (= 0 (substring-index "" "")))
@@ -62,6 +64,7 @@
 (assert-error (substring-ci=? "a" "a" 0 -2))
 (assert-error (substring-ci=? "a" "a" 0 0 2))
 (assert-error (substring-ci=? "a" "a" 0 0 -2))
+(assert-error (substring-ci=? "a" "a" 0 0 2))
 (assert-error (substring-index "" "a" 2))
 (assert-error (substring-index "a" "b" 2))
 (assert (not (substring-index "a" "b" 1)))
-- 
2.40.0

Reply via email to