chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] [PATCH] Restore old read-line of zero bytes behaviour


From: Peter Bex
Subject: [Chicken-hackers] [PATCH] Restore old read-line of zero bytes behaviour when FD is not ready
Date: Sun, 29 Jul 2018 17:40:14 +0200
User-agent: NeoMutt/20170113 (1.7.2)

Hi all,

While porting http-client, I ran into one very puzzling bug in CHICKEN 5:
one particular test would consistently fail, for no good reason.  After
some head scratching and debugging, I noticed that this broke by a subtle
and (I think) unintended change that was part of c78cdcd9.  It's the first
hunk of that patch that introduced the unintended change:

commit c78cdcd9347a1e501ccc8882cd8c1cadeb76f0c3
Author: Evan Hanson <address@hidden>
Date:   Mon Jan 18 23:31:53 2016 +1300

    Make `read-string` return #!eof on empty input

diff --git a/extras.scm b/extras.scm
index a766d209..3b337981 100644
--- a/extras.scm
+++ b/extras.scm
@@ -165,7 +165,9 @@
 (define ##sys#read-string/port
   (lambda (n p)
     (##sys#check-input-port p #t 'read-string)
-    (cond (n (##sys#check-fixnum n 'read-string)
+    (cond ((eof-object? (##sys#peek-char-0 p))
+          (if (eq? n 0) "" #!eof))
+          (n (##sys#check-fixnum n 'read-string)
             (let* ((str (##sys#make-string n))
                    (n2 (##sys#read-string! n str p 0)) )
               (if (eq? n n2)
                   str
                   (##sys#substring str 0 n2))))

The old code would immediately return an empty string because of
the first check in ##sys#read-string!:

  (cond ((eq? n 0) 0)

The returned zero here is the number of bytes read.  So the code
does nothing to the FD at all.

In the new version, we call ##sys#peek-char-0, which *should* not
be different semantically, but it may cause a read timeout if there's
nothing offered on the port.  I think if we ask for zero bytes, we
shouldn't even attempt that.  Besides, even in the new code we still
let read-string return "" when asking for zero bytes, instead of #!eof,
so I think the peek-char is unnecessary and in the case of an FD that
isn't ready, even wrong.

The attached patch restores this behaviour so that the http-client tests
succeed.

Cheers,
Peter

Attachment: 0001-Restore-read-string-semantics-when-reading-0-bytes-b.patch
Description: Text Data

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]