From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.devel Subject: `scm_c_read ()' and `swap_buffer' trick harmful Date: Sat, 15 Nov 2008 21:04:32 +0100 Message-ID: <87vduo92nj.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1226779510 14036 80.91.229.12 (15 Nov 2008 20:05:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 15 Nov 2008 20:05:10 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Nov 15 21:06:14 2008 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1L1RPF-0006PQ-FB for guile-devel@m.gmane.org; Sat, 15 Nov 2008 21:06:09 +0100 Original-Received: from localhost ([127.0.0.1]:50296 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L1RO7-0008R1-7e for guile-devel@m.gmane.org; Sat, 15 Nov 2008 15:04:59 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L1RO3-0008QA-E5 for guile-devel@gnu.org; Sat, 15 Nov 2008 15:04:55 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L1RO2-0008PO-1J for guile-devel@gnu.org; Sat, 15 Nov 2008 15:04:55 -0500 Original-Received: from [199.232.76.173] (port=41693 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L1RO1-0008PH-IH for guile-devel@gnu.org; Sat, 15 Nov 2008 15:04:53 -0500 Original-Received: from main.gmane.org ([80.91.229.2]:51550 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1L1RO1-00020E-6x for guile-devel@gnu.org; Sat, 15 Nov 2008 15:04:53 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1L1RNw-000661-HK for guile-devel@gnu.org; Sat, 15 Nov 2008 20:04:48 +0000 Original-Received: from reverse-83.fdn.fr ([80.67.176.83]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 15 Nov 2008 20:04:48 +0000 Original-Received: from ludo by reverse-83.fdn.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 15 Nov 2008 20:04:48 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 63 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: reverse-83.fdn.fr X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 25 Brumaire an 217 de la =?iso-8859-1?Q?R=E9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 821D 815D 902A 7EAB 5CEE D120 7FBA 3D4F EB1F 5364 X-OS: i686-pc-linux-gnu User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (gnu/linux) Cancel-Lock: sha1:a2zemxRyRkJyEpf9A56yOepNHhA= X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:7843 Archived-At: Hello! I just discovered undesirable side effects of commit b5cb4464ca4e23d077a9777bbc17835feb0f4374 "Make multi-byte reads on unbuffered ports more efficient." An example application that breaks in the presence of this patch are "custom binary input ports" (aka. CBIPs [0]) in Guile-R6RS-Libs [1]. The CBIP implementation [2] works as follows: 1. make_cbip () /* Create a bytevector for use as the CBIP's internal buffer. */ SCM bv = scm_r6rs_c_make_bytevector (c_len); c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv; c_port->read_end = (unsigned char *) c_bv; /* Store BV for later reuse. */ SCM_SETSTREAM (port, SCM_UNPACK (bv, and other things)); 2. cbip_fill_input (port) if (c_port->read_pos >= c_port->read_end) { /* Invoke the user's `read!' procedure. */ bv = SCM_R6RS_CBIP_BYTEVECTOR (port); octets = scm_call_3 (read_proc, bv, SCM_INUM0, SCM_I_MAKINUM (CBIP_BUFFER_SIZE)); c_octets = scm_to_uint (octets); c_port->read_pos = (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); c_port->read_end = (unsigned char *) c_port->read_pos + c_port->c_octets; } IOW, the CBIP `fill_input' method does *not* directly pass `c_port->read_buf' to the user's `read!' method but instead passes it its bytevector, which it assumes to wrap its internal. Thus, if `c_port->read_buf' happens to point to something other than BV's contents, it is just left untouched. Worse, `cbip_fill_input ()' updates `read_pos' and `read_end' but does not touch `read_buf', leading to an inconsistent state that will confuse later `scm_fill_input ()' calls on that port (e.g., in the loop for `scm_c_read ()'), and possibly to heap corruption. So where to go from here? I think this example shows that the `swap_buffer' trick is too risky, unfortunately. Thus, we may need to revert it, at least in 1.8. Second, I think that a `read' method as a replacement for `fill_input', as I proposed back then [3], would be safer; maybe 1.9 would be a nice place to add it. Neil: what do you think? Thanks, Ludo'. [0] http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-1.html#node_toc_node_sec_8.2.7 [1] http://www.fdn.fr/~lcourtes/software/guile/guile-r6rs-libs-0.1.tar.gz and http://repo.or.cz/w/guile-r6rs-libs.git [2] http://repo.or.cz/w/guile-r6rs-libs.git?a=blob;f=src/ports.c;#l210 [3] http://thread.gmane.org/gmane.lisp.guile.devel/7292