~ chicken-core (master) eca57e92a9b9ebdd341487902cf0a05edb747765


commit eca57e92a9b9ebdd341487902cf0a05edb747765
Author:     felix <felix@call-with-current-continuation.org>
AuthorDate: Wed Oct 14 13:19:24 2026 +0200
Commit:     felix <felix@call-with-current-continuation.org>
CommitDate: Wed Oct 14 13:19:24 2026 +0200

    count full and incomplete sequences fully when reading strings
    
    This also makes the fragment counting routine check UTF continuation bits,
    if the bits are missing, the initial part is counted as full codepoints.

diff --git a/library.scm b/library.scm
index df7d43d0..3f31a0b5 100644
--- a/library.scm
+++ b/library.scm
@@ -8378,12 +8378,12 @@ static C_word C_curdir(C_word buf, C_word size) {
                         (lambda (dbuf start len)
                           (##core#inline "C_copy_memory_with_offset" buf dbuf p start len)
                           len)))))
-       (define (finish un bytes)
+       (define (finish un bytes left)
          (##core#inline "C_utf_overwrite" dest start un buf bytes)
-         un)
-       (let loop ((p 0) (n n) (un 0) (bn 0))    ; pos, count, codepoints, rest bytes
+         (fx+ un left))
+       (let loop ((p 0) (n n) (un 0) (bn 0))    ; pos, count, codepoints, total bytes
          (let ((bytes (readb n buf port p)))
-           (cond ((eq? bytes 0) (finish un bn))
+           (cond ((eq? bytes 0) (finish un bn 0))
                  ((eq? enc 'utf-8)
                   ;; read incomplete fragments
                   ;; FIXME: hardcoded, should be encoding-specific!
@@ -8397,11 +8397,10 @@ static C_word C_curdir(C_word buf, C_word size) {
                       (cond ((fx> left 0)
                              (let ((b2 (readb left buf port (fx+ p bytes))))
                                (if (fx< b2 left)
-                                   (finish total tbytes)
+                                   (finish total tbytes (fx+ left b2))
                                    (recount (fx+ bytes b2)))))
-                            ((eq? remain 0) (finish total tbytes))
-                            (else (loop (fx+ p bytes) remain total
-                                        tbytes))))))
+                            ((eq? remain 0) (finish total tbytes 0))
+                            (else (loop (fx+ p bytes) remain total tbytes))))))
                  (else
                   (loop (fx+ p bytes) (fx- n bytes)
                         (fx+ un bytes) (fx+ bn bytes))))))))))
diff --git a/tests/port-tests.scm b/tests/port-tests.scm
index 3d195432..919ddf02 100644
--- a/tests/port-tests.scm
+++ b/tests/port-tests.scm
@@ -1,10 +1,10 @@
 (import chicken.condition chicken.file chicken.file.posix
 	chicken.flonum chicken.format chicken.io chicken.port
-        chicken.bytevector
+        chicken.bytevector chicken.port 
 	chicken.process chicken.process.signal chicken.tcp chicken.number-vector)
 
 (import (only (scheme base) input-port-open? output-port-open? open-input-string
-              write-string open-output-string get-output-string
+              write-string open-output-string get-output-string open-input-bytevector
               flush-output-port peek-u8 u8-ready? read-u8 write-u8))
 
 (include "test.scm")
@@ -431,6 +431,23 @@ EOF
                 buf
                 "89067")))
 
+(test-group "read incomplete string from bytevector"
+  (test-equal "one byte, incomplete 2 byte sequence"
+              (map char->integer (string->list (read-string 10 (open-input-bytevector #u8(#xC3)))))
+              '(#xdcc3))
+  (test-equal "two bytes, incomplete 2 byte sequence"
+              (map char->integer (string->list (read-string 10 (open-input-bytevector #u8(#xC3 65)))))
+              '(#xdcc3 65))
+  (test-equal "three bytes, incomplete 2 byte sequence"
+              (map char->integer (string->list (read-string 10 (open-input-bytevector #u8(#xC3 65 66)))))
+              '(#xdcc3 65 66))
+  (test-equal "three bytes, complete 2 byte sequence"
+              (map char->integer (string->list (read-string 10 (open-input-bytevector #u8(#xC3 #x81 65)))))
+              '(193 65))
+  (test-equal "four bytes, complete 2 byte sequence"
+              (map char->integer (string->list (read-string 10 (open-input-bytevector #u8(#xC3 #x81 65 66)))))
+              '(193 65 66)))
+
 (test-group "line endings"
   (let ((s "foo\nbar\rbaz\r\nqux")
 	(f (lambda ()
diff --git a/utf.c b/utf.c
index b52c7227..8ff36e69 100644
--- a/utf.c
+++ b/utf.c
@@ -603,18 +603,25 @@ C_regparm C_word C_utf_fragment_counts(C_word bv, C_word pos, C_word len)
     int full = 0;
     C_uchar *ptr = C_data_pointer(bv) + C_unfix(pos);
     int count = C_unfix(len);
+    unsigned int byte;
 
     while(count) {
-        unsigned int byte = *(ptr++);
+next:
+        byte = *(ptr++);
         int n = lengths[ byte >> 3 ];
         int bn = n + !n;
         if(count >= bn) {
+            for(int cont = bn - 1; cont > 0; --cont) {
+                if((*(ptr++) & 0x80) == 0) {
+                    full += bn - cont;
+                    count -= cont;
+                    goto next;
+                }
+            }
             ++full;
             count -= bn;
-            ptr += bn - 1;
         } else return C_fix((full << 4) | (bn - count));
     }
-
     return C_fix(full << 4);
 }
 
Trap