~ chicken-core (master) 03039eef925100b360f4abcb83ce469f9c1a25be
commit 03039eef925100b360f4abcb83ce469f9c1a25be
Author: Felix Winkelmann <felix.winkelmann@bevuta.com>
AuthorDate: Sun Sep 13 23:37:32 2026 +0200
Commit: Felix Winkelmann <felix.winkelmann@bevuta.com>
CommitDate: Sun Sep 13 23:37:32 2026 +0200
add range checks for start, avoid duplicate decoding
diff --git a/library.scm b/library.scm
index 39465dda..9c4ddbcc 100644
--- a/library.scm
+++ b/library.scm
@@ -3737,16 +3737,22 @@ EOF
(##sys#check-bytevector bv 'utf8->string)
(let* ((n (##sys#size bv))
(to (or end n)))
+ (##sys#check-range/including start 0 n 'utf8->string)
(if end
(##sys#check-range/including end 0 n 'utf8->string))
- (if (not (##core#inline "C_utf_validate" bv n start to))
- (##sys#error-hook (foreign-value "C_DECODING_ERROR" int) 'utf8->string bv))
- (##sys#buffer->string bv start (##core#inline "C_fixnum_difference" to start))))
+ (let ((count (##core#inline "C_utf_validate" bv n start to)))
+ (if (not count)
+ (##sys#error-hook (foreign-value "C_DECODING_ERROR" int) 'utf8->string bv))
+ (let* ((len (##core#inline "C_fixnum_difference" to start))
+ (dest (##sys#make-bytevector (##core#inline "C_fixnum_plus" len 1))))
+ (##core#inline "C_copy_memory_with_offset" dest bv 0 start len)
+ (##core#inline_allocate ("C_a_ustring" 5) dest count)))))
(define (bytes->string bv #!optional (start 0) end)
(##sys#check-bytevector bv 'bytes->string)
(let* ((n (##sys#size bv))
(to (or end n)))
+ (##sys#check-range/including start 0 n 'bytes->string)
(if end
(##sys#check-range/including end 0 n 'bytes->string))
(##sys#buffer->string bv start (##core#inline "C_fixnum_difference" to start))))
Trap