Subject: Re: bug#20302: peek-char messes up file position on binary string ports

From: Mark H Weaver <[email-masked]>
Date: Fri, 17 Apr 2015 01:29:08 -0400

David Kastrup <[email-masked]> writes:

> (use-modules (rnrs bytevectors) (rnrs io ports))
> (let ((port (open-bytevector-input-port
> 	     (string->utf8 "Blablabla\nBlablabla\n"))))
>   (seek port 13 SEEK_SET)
>   (format #t "~c ~d\n" (peek-char port)
> 	  (ftell port)))
> ;; Outputs b 3 but should output b 13
>
> This is using
> guile (GNU Guile) 2.0.11
> Packaged by Debian (2.0.11-deb+1-1)

Ouch :-(

The problem is that r6rs-ports.c:bip_seek assumes that
c_port->read_{buf,pos,end} point to the original bytevector, and fail to
handle the case where it points to a "putback" buffer.

Note that (ftell port) is equivalent to (seek port 0 SEEK_CUR).

> Any idea how to work around _this_ one?

While investigating possible workarounds, I noticed that, afaict, we did
_not_ make the incompatible change in 2.0.11 that you claim we made in
<http://bugs.gnu.org/20109>.  That change was only made on our master
branch, which will become 2.2.

In response to that bug report, I suggested that you use bytevector
input ports instead of string ports, and that's what led you to hit this
problem.

Now my suggestion is that you continue using string ports in 2.0.11 or
earlier, and use bytevector inputs ports in 2.0.12 or later.  I'll make
sure this bug is fixed in 2.0.12.

Please let me know if you think I'm mistaken in any of this.  If so, I
have some other possible workarounds to offer, but I think they will not
be needed.

      Mark