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: Re: `scm_c_read ()' and `swap_buffer' trick harmful Date: Sat, 22 Nov 2008 16:02:47 +0100 Message-ID: <87y6zbolbc.fsf@gnu.org> References: <87vduo92nj.fsf@gnu.org> <49dd78620811200522k315281c8m9d952a17145b2479@mail.gmail.com> <871vx6ik42.fsf@gnu.org> <49dd78620811201425j5dcd7e0g4165c8ea4c6b08ce@mail.gmail.com> <87fxllt3f7.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1227366207 28839 80.91.229.12 (22 Nov 2008 15:03:27 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 22 Nov 2008 15:03:27 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Nov 22 16:04:29 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 1L3u29-0008G3-0H for guile-devel@m.gmane.org; Sat, 22 Nov 2008 16:04:29 +0100 Original-Received: from localhost ([127.0.0.1]:41982 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L3u0z-0002vb-JI for guile-devel@m.gmane.org; Sat, 22 Nov 2008 10:03:17 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L3u0n-0002uX-5F for guile-devel@gnu.org; Sat, 22 Nov 2008 10:03:05 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L3u0k-0002tS-9y for guile-devel@gnu.org; Sat, 22 Nov 2008 10:03:04 -0500 Original-Received: from [199.232.76.173] (port=39356 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L3u0i-0002tF-UD for guile-devel@gnu.org; Sat, 22 Nov 2008 10:03:01 -0500 Original-Received: from main.gmane.org ([80.91.229.2]:50553 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 1L3u0i-0007ns-5R for guile-devel@gnu.org; Sat, 22 Nov 2008 10:03:00 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1L3u0f-00072K-Do for guile-devel@gnu.org; Sat, 22 Nov 2008 15:02:57 +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, 22 Nov 2008 15:02:57 +0000 Original-Received: from ludo by reverse-83.fdn.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 22 Nov 2008 15:02:57 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 210 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: 2 Frimaire 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:XJR5zj9fC/ePxIqcCsfRSDZtM+k= 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:7883 Archived-At: --=-=-= Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Hello, ludo@gnu.org (Ludovic Courtès) writes: > Thanks for the quick fix! Attach is a test case for this, please apply (with git-am(1)) and commit right after your fix. Once we're done with this, I think we should release 1.8.6. I'm should be able to do it this week-end. Thanks, Ludo'. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-C-unit-test-for-scm_c_read-and-the-port-API.patch Content-Description: The patch >From ca72bdfec2457b035836bc43afbc5ea20fe8af02 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Ludovic=20Court=C3=A8s?= Date: Sat, 22 Nov 2008 15:48:16 +0100 Subject: [PATCH] Add C unit test for `scm_c_read ()' and the port API. * test-suite/standalone/Makefile.am (check_PROGRAMS, TESTS): Add `test-scm-c-read'. (test_scm_c_read_SOURCES, test_scm_c_read_CFLAGS, test_scm_c_read_LDADD): New. --- test-suite/standalone/.gitignore | 1 + test-suite/standalone/Makefile.am | 7 ++ test-suite/standalone/test-scm-c-read.c | 130 +++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 0 deletions(-) create mode 100644 test-suite/standalone/test-scm-c-read.c diff --git a/test-suite/standalone/.gitignore b/test-suite/standalone/.gitignore index 78af304..b1f4088 100644 --- a/test-suite/standalone/.gitignore +++ b/test-suite/standalone/.gitignore @@ -7,3 +7,4 @@ /test-with-guile-module /test-use-srfi /test-scm-with-guile +/test-scm-c-read diff --git a/test-suite/standalone/Makefile.am b/test-suite/standalone/Makefile.am index 6ae5e88..3afd7e5 100644 --- a/test-suite/standalone/Makefile.am +++ b/test-suite/standalone/Makefile.am @@ -107,6 +107,13 @@ TESTS += test-conversion check_SCRIPTS += test-use-srfi TESTS += test-use-srfi +# test-scm-c-read +test_scm_c_read_SOURCES = test-scm-c-read.c +test_scm_c_read_CFLAGS = ${test_cflags} +test_scm_c_read_LDADD = ${top_builddir}/libguile/libguile.la +check_PROGRAMS += test-scm-c-read +TESTS += test-scm-c-read + if BUILD_PTHREAD_SUPPORT # test-with-guile-module diff --git a/test-suite/standalone/test-scm-c-read.c b/test-suite/standalone/test-scm-c-read.c new file mode 100644 index 0000000..be8ea80 --- /dev/null +++ b/test-suite/standalone/test-scm-c-read.c @@ -0,0 +1,130 @@ +/* Copyright (C) 2008 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* Exercise `scm_c_read ()' and the port type API. Verify assumptions that + can be made by port type implementations. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + + + +/* Size of our port's internal buffer. */ +#define PORT_BUFFER_SIZE 1024 + +/* Return a new port of type PORT_TYPE. */ +static inline SCM +make_port (scm_t_bits port_type) +{ + SCM port; + char *c_buffer; + scm_t_port *c_port; + + c_buffer = scm_gc_calloc (PORT_BUFFER_SIZE, "custom-port-buffer"); + + port = scm_new_port_table_entry (port_type); + + /* Attach it the underlying u8vector, which we'll use as PORT's buffer. */ + SCM_SETSTREAM (port, (scm_t_bits) c_buffer); + + /* Have the port directly access the buffer (bytevector). */ + c_port = SCM_PTAB_ENTRY (port); + c_port->read_pos = c_port->read_buf = (unsigned char *) c_buffer; + c_port->read_end = (unsigned char *) c_buffer + PORT_BUFFER_SIZE; + c_port->read_buf_size = PORT_BUFFER_SIZE; + + /* Mark PORT as open and readable. */ + SCM_SET_CELL_TYPE (port, port_type | SCM_OPN | SCM_RDNG); + + return port; +} + +/* Read one byte from PORT. */ +static int +fill_input (SCM port) +{ + int result; + scm_t_port *c_port = SCM_PTAB_ENTRY (port); + + /* Make sure that C_PORT's internal buffer wasn't changed behind our back. + See http://lists.gnu.org/archive/html/guile-devel/2008-11/msg00042.html + for an example where this assumption matters. */ + assert (c_port->read_buf == (unsigned char *) SCM_STREAM (port)); + assert (c_port->read_buf_size == PORT_BUFFER_SIZE); + + if (c_port->read_pos >= c_port->read_end) + result = EOF; + else + result = (int) *c_port->read_pos++; + + return result; +} + +/* Return true (non-zero) if BUF contains only zeros. */ +static inline int +zeroed_buffer_p (const char *buf, size_t len) +{ + size_t i; + + for (i = 0; i < len; i++) + if (buf[i] != 0) + return 0; + + return 1; +} + +/* Run the test. */ +static void * +do_start (void *arg) +{ + SCM port; + scm_t_bits port_type; + char buffer[PORT_BUFFER_SIZE + (PORT_BUFFER_SIZE / 2)]; + size_t read, last_read; + + port_type = scm_make_port_type ("custom-input-port", fill_input, NULL); + port = make_port (port_type); + + read = 0; + do + { + last_read = scm_c_read (port, &buffer[read], 123); + assert (last_read <= 123); + assert (zeroed_buffer_p (&buffer[read], last_read)); + + read += last_read; + } + while (last_read > 0 && read < sizeof (buffer)); + + /* We shouldn't be able to read more than what's in PORT's buffer. */ + assert (read == PORT_BUFFER_SIZE); + + return NULL; +} + + +int +main (int argc, char *argv[]) +{ + scm_with_guile (do_start, NULL); + + return 0; +} -- 1.6.0.3 --=-=-=--