From debbugs-submit-bounces@debbugs.gnu.org Sat Apr 11 07:48:24 2015 Received: (at submit) by debbugs.gnu.org; 11 Apr 2015 11:48:25 +0000 Received: from localhost ([127.0.0.1]:52940 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Ygttg-0003t9-Ng for submit@debbugs.gnu.org; Sat, 11 Apr 2015 07:48:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:38590) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Ygtte-0003sp-NI for submit@debbugs.gnu.org; Sat, 11 Apr 2015 07:48:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YgttY-0003kp-Ng for submit@debbugs.gnu.org; Sat, 11 Apr 2015 07:48:17 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_05,T_RP_MATCHES_RCVD autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:60290) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YgttY-0003kf-Km for submit@debbugs.gnu.org; Sat, 11 Apr 2015 07:48:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52097) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YgttX-0001qp-SH for bug-guile@gnu.org; Sat, 11 Apr 2015 07:48:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YgttX-0003hl-5T for bug-guile@gnu.org; Sat, 11 Apr 2015 07:48:15 -0400 Received: from fencepost.gnu.org ([2001:4830:134:3::e]:38108) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YgttX-0003hI-1h for bug-guile@gnu.org; Sat, 11 Apr 2015 07:48:15 -0400 Received: from localhost ([127.0.0.1]:54269 helo=lola) by fencepost.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YgttW-0003KQ-IR for bug-guile@gnu.org; Sat, 11 Apr 2015 07:48:14 -0400 Received: by lola (Postfix, from userid 1000) id 144DDE0645; Sat, 11 Apr 2015 13:48:14 +0200 (CEST) From: David Kastrup To: bug-guile@gnu.org Subject: peek-char messes up file position on binary string ports Date: Sat, 11 Apr 2015 13:48:14 +0200 Message-ID: <87y4lylwch.fsf@fencepost.gnu.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) --=-=-= Content-Type: text/plain Any idea how to work around _this_ one? --=-=-= Content-Type: text/plain Content-Disposition: inline; filename=gaga.scm (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 --=-=-= Content-Type: text/plain This is using guile (GNU Guile) 2.0.11 Packaged by Debian (2.0.11-deb+1-1) -- David Kastrup --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Fri Apr 17 01:28:38 2015 Received: (at 20302) by debbugs.gnu.org; 17 Apr 2015 05:28:38 +0000 Received: from localhost ([127.0.0.1]:58491 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YiypS-0002tS-1h for submit@debbugs.gnu.org; Fri, 17 Apr 2015 01:28:38 -0400 Received: from world.peace.net ([50.252.239.5]:53919 ident=hope5) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YiypQ-0002tL-KJ for 20302@debbugs.gnu.org; Fri, 17 Apr 2015 01:28:37 -0400 Received: from [10.1.10.78] (helo=jojen) by world.peace.net with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1YiypJ-0008FI-QT; Fri, 17 Apr 2015 01:28:29 -0400 From: Mark H Weaver To: David Kastrup Subject: Re: bug#20302: peek-char messes up file position on binary string ports References: <87y4lylwch.fsf@fencepost.gnu.org> Date: Fri, 17 Apr 2015 01:29:08 -0400 In-Reply-To: <87y4lylwch.fsf@fencepost.gnu.org> (David Kastrup's message of "Sat, 11 Apr 2015 13:48:14 +0200") Message-ID: <87383z5nmj.fsf@netris.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 20302 Cc: 20302@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 0.0 (/) David Kastrup 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 . 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 From debbugs-submit-bounces@debbugs.gnu.org Mon Aug 31 05:09:33 2015 Received: (at 20302) by debbugs.gnu.org; 31 Aug 2015 09:09:33 +0000 Received: from localhost ([127.0.0.1]:43432 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZWL5p-0008Te-HW for submit@debbugs.gnu.org; Mon, 31 Aug 2015 05:09:33 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36344) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZWL5o-0008TX-Ky for 20302@debbugs.gnu.org; Mon, 31 Aug 2015 05:09:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZWL5n-0006oN-R2 for 20302@debbugs.gnu.org; Mon, 31 Aug 2015 05:09:32 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00, T_RP_MATCHES_RCVD autolearn=disabled version=3.3.2 Received: from fencepost.gnu.org ([208.118.235.10]:48753) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWL5n-0006oJ-OH for 20302@debbugs.gnu.org; Mon, 31 Aug 2015 05:09:31 -0400 Received: from localhost ([127.0.0.1]:33555 helo=lola) by fencepost.gnu.org with esmtp (Exim 4.82) (envelope-from ) id 1ZWL5m-0007fb-VT for 20302@debbugs.gnu.org; Mon, 31 Aug 2015 05:09:31 -0400 Received: by lola (Postfix, from userid 1000) id D58A6E3665; Mon, 31 Aug 2015 11:09:09 +0200 (CEST) From: David Kastrup To: 20302@debbugs.gnu.org Subject: Re: bug#20302: Acknowledgement (peek-char messes up file position on binary string ports) References: <87y4lylwch.fsf@fencepost.gnu.org> Date: Mon, 31 Aug 2015 11:09:09 +0200 In-Reply-To: (GNU bug Tracking System's message of "Sat, 11 Apr 2015 11:49:02 +0000") Message-ID: <8737yz7rka.fsf@fencepost.gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.10 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: 20302 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) > I'll make sure this bug is fixed in 2.0.12. Any perspective on this yet? It's been more than 4 months. -- David Kastrup From debbugs-submit-bounces@debbugs.gnu.org Sun Sep 06 07:56:28 2015 Received: (at 20302) by debbugs.gnu.org; 6 Sep 2015 11:56:28 +0000 Received: from localhost ([127.0.0.1]:50530 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZYYYd-0001bi-TU for submit@debbugs.gnu.org; Sun, 06 Sep 2015 07:56:28 -0400 Received: from world.peace.net ([50.252.239.5]:53998) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZYYYa-0001bC-Gf for 20302@debbugs.gnu.org; Sun, 06 Sep 2015 07:56:25 -0400 Received: from c-24-61-130-184.hsd1.ma.comcast.net ([24.61.130.184] helo=yeeloong) by world.peace.net with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1ZYYXx-0007ol-By; Sun, 06 Sep 2015 07:55:45 -0400 From: Mark H Weaver To: David Kastrup Subject: Re: bug#20302: peek-char messes up file position on binary string ports References: <87y4lylwch.fsf@fencepost.gnu.org> <87383z5nmj.fsf@netris.org> Date: Sun, 06 Sep 2015 07:55:02 -0400 In-Reply-To: <87383z5nmj.fsf@netris.org> (Mark H. Weaver's message of "Fri, 17 Apr 2015 01:29:08 -0400") Message-ID: <87oahfoj8p.fsf@netris.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 20302 Cc: 20302@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 0.0 (/) --=-=-= Content-Type: text/plain Mark H Weaver writes: > David Kastrup 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). I've attached a preliminary patch set to fix this bug and some others. I'm going to hold off on pushing them to stable-2.0 until I've added tests and convinced myself that I haven't introduced any new problems. Mark --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-build-Add-SCM_T_OFF_MAX-and-SCM_T_OFF_MIN-to-scmconf.patch Content-Description: [PATCH 1/3] build: Add SCM_T_OFF_MAX and SCM_T_OFF_MIN to scmconfig.h >From 1d398a5ee910f3edf17b8b86e29a7fbe967071ec Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Sun, 6 Sep 2015 07:33:55 -0400 Subject: [PATCH 1/3] build: Add SCM_T_OFF_MAX and SCM_T_OFF_MIN to scmconfig.h. * libguile/gen-scmconfig.c (main): Add SCM_T_OFF_MAX and SCM_T_OFF_MIN to the generated 'scmconfig.h' file. --- libguile/gen-scmconfig.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libguile/gen-scmconfig.c b/libguile/gen-scmconfig.c index 2f6fa6e..e433bd1 100644 --- a/libguile/gen-scmconfig.c +++ b/libguile/gen-scmconfig.c @@ -376,10 +376,16 @@ main (int argc, char *argv[]) #if defined GUILE_USE_64_CALLS && defined HAVE_STAT64 pf ("typedef scm_t_int64 scm_t_off;\n"); + pf ("#define SCM_T_OFF_MAX SCM_T_INT64_MAX\n"); + pf ("#define SCM_T_OFF_MIN SCM_T_INT64_MIN\n"); #elif SIZEOF_OFF_T == SIZEOF_INT pf ("typedef int scm_t_off;\n"); + pf ("#define SCM_T_OFF_MAX INT_MAX\n"); + pf ("#define SCM_T_OFF_MIN INT_MIN\n"); #else pf ("typedef long int scm_t_off;\n"); + pf ("#define SCM_T_OFF_MAX LONG_MAX\n"); + pf ("#define SCM_T_OFF_MIN LONG_MIN\n"); #endif pf ("/* Define to 1 if the compiler supports the " -- 2.5.0 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-PRELIMINARY-Fix-seeking-on-binary-input-ports-with-p.patch Content-Description: [PATCH 2/3] PRELIMINARY: Fix seeking on binary input ports with putback buffers >From 53a6a5f7a66ac1f7b92a7347dc9486db14c73df5 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Sun, 6 Sep 2015 07:35:58 -0400 Subject: [PATCH 2/3] PRELIMINARY: Fix seeking on binary input ports with putback buffers. Fixes . Reported by David Kastrup . * libguile/r6rs-ports.c (bip_end_input): New static function. (initialize_bytevector_input_ports): Register it. (bip_seek): Rewrite to handle putback buffers, based on st_seek from strports.c. --- libguile/r6rs-ports.c | 83 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/libguile/r6rs-ports.c b/libguile/r6rs-ports.c index a17b7b4..1bf766c 100644 --- a/libguile/r6rs-ports.c +++ b/libguile/r6rs-ports.c @@ -125,45 +125,69 @@ bip_fill_input (SCM port) return result; } +static void +bip_end_input (SCM port, int offset) +{ + scm_t_port *c_port = SCM_PTAB_ENTRY (port); + + if (c_port->read_pos - c_port->read_buf < offset) + scm_misc_error ("bip_end_input", "negative position", SCM_EOL); + + c_port->read_pos -= offset; +} + static scm_t_off bip_seek (SCM port, scm_t_off offset, int whence) #define FUNC_NAME "bip_seek" { - scm_t_off c_result = 0; scm_t_port *c_port = SCM_PTAB_ENTRY (port); + scm_t_off target; - switch (whence) + if (offset == 0 && whence == SEEK_CUR) + /* special case to avoid disturbing the putback buffer. */ { - case SEEK_CUR: - offset += c_port->read_pos - c_port->read_buf; - /* Fall through. */ - - case SEEK_SET: - if (c_port->read_buf + offset <= c_port->read_end) - { - c_port->read_pos = c_port->read_buf + offset; - c_result = offset; - } + if (c_port->read_buf == c_port->putback_buf) + target = c_port->saved_read_pos - c_port->saved_read_buf + - (c_port->read_end - c_port->read_pos); else - scm_out_of_range (FUNC_NAME, scm_from_int (offset)); - break; + target = c_port->read_pos - c_port->read_buf; + } + else + { + scm_t_off base = 0; - case SEEK_END: - if (c_port->read_end - offset >= c_port->read_buf) - { - c_port->read_pos = c_port->read_end - offset; - c_result = c_port->read_pos - c_port->read_buf; - } - else - scm_out_of_range (FUNC_NAME, scm_from_int (offset)); - break; + /* If the putback buffer is currently active, this will dump its + contents, switch back to the main read buffer, and move + read_pos backwards as needed to account for the bytes that were + put back. */ + if (c_port->read_buf == c_port->putback_buf) + scm_end_input (port); - default: - scm_wrong_type_arg_msg (FUNC_NAME, 0, port, - "invalid `seek' parameter"); - } + switch (whence) + { + case SEEK_CUR: + base = c_port->read_pos - c_port->read_buf; + break; + case SEEK_END: + base = c_port->read_end - c_port->read_buf; + break; + case SEEK_SET: + base = 0; + break; + default: + scm_wrong_type_arg_msg (FUNC_NAME, 0, port, + "invalid `whence' argument"); + } - return c_result; + if (offset > SCM_T_OFF_MAX - base) /* Overflow check */ + scm_out_of_range (FUNC_NAME, scm_from_int (offset)); + target = base + offset; + if (target < 0 || target > c_port->read_end - c_port->read_buf) + scm_out_of_range (FUNC_NAME, scm_from_int (offset)); + + c_port->read_pos = c_port->read_buf + target; + } + return target; } #undef FUNC_NAME @@ -176,7 +200,8 @@ initialize_bytevector_input_ports (void) scm_make_port_type ("r6rs-bytevector-input-port", bip_fill_input, NULL); - scm_set_port_seek (bytevector_input_port_type, bip_seek); + scm_set_port_end_input (bytevector_input_port_type, bip_end_input); + scm_set_port_seek (bytevector_input_port_type, bip_seek); } -- 2.5.0 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0003-PRELIMINARY-string-ports-Add-overflow-checks-and-oth.patch Content-Description: [PATCH 3/3] PRELIMINARY: string ports: Add overflow checks and other fixes >From dc09359733feb68590c905d77e2172ec6e3781d0 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Sun, 6 Sep 2015 07:42:07 -0400 Subject: [PATCH 3/3] PRELIMINARY: string ports: Add overflow checks and other fixes. * libguile/strports.c (st_resize_port): Check that 'new_size' fits in a size_t. (st_end_input): Improve code clarity. (st_seek): Check for overflow during computation of target position. Check for invalid 'whence' argument. Resize the port when seeking to a position beyond the end of the buffer. Check for overflow during computation of new buffer size when resizing the port. --- libguile/strports.c | 64 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/libguile/strports.c b/libguile/strports.c index f306019..18f1970 100644 --- a/libguile/strports.c +++ b/libguile/strports.c @@ -103,28 +103,33 @@ stfill_buffer (SCM port) static void st_resize_port (scm_t_port *pt, scm_t_off new_size) { - SCM old_stream = SCM_PACK (pt->stream); - const signed char *src = SCM_BYTEVECTOR_CONTENTS (old_stream); - SCM new_stream = scm_c_make_bytevector (new_size); - signed char *dst = SCM_BYTEVECTOR_CONTENTS (new_stream); - unsigned long int old_size = SCM_BYTEVECTOR_LENGTH (old_stream); - unsigned long int min_size = min (old_size, new_size); + if (new_size < 0 || new_size > SCM_I_SIZE_MAX) + scm_misc_error ("st_resize_port", "new_size overflow", SCM_EOL); - scm_t_off index = pt->write_pos - pt->write_buf; + { + SCM old_stream = SCM_PACK (pt->stream); + const signed char *src = SCM_BYTEVECTOR_CONTENTS (old_stream); + SCM new_stream = scm_c_make_bytevector (new_size); + signed char *dst = SCM_BYTEVECTOR_CONTENTS (new_stream); + unsigned long int old_size = SCM_BYTEVECTOR_LENGTH (old_stream); + unsigned long int min_size = min (old_size, new_size); - pt->write_buf_size = new_size; + scm_t_off index = pt->write_pos - pt->write_buf; - memcpy (dst, src, min_size); + pt->write_buf_size = new_size; - scm_remember_upto_here_1 (old_stream); + memcpy (dst, src, min_size); - /* reset buffer. */ - { - pt->stream = SCM_UNPACK (new_stream); - pt->read_buf = pt->write_buf = (unsigned char *)dst; - pt->read_pos = pt->write_pos = pt->write_buf + index; - pt->write_end = pt->write_buf + pt->write_buf_size; - pt->read_end = pt->read_buf + pt->read_buf_size; + scm_remember_upto_here_1 (old_stream); + + /* reset buffer. */ + { + pt->stream = SCM_UNPACK (new_stream); + pt->read_buf = pt->write_buf = (unsigned char *)dst; + pt->read_pos = pt->write_pos = pt->write_buf + index; + pt->write_end = pt->write_buf + pt->write_buf_size; + pt->read_end = pt->read_buf + pt->read_buf_size; + } } } @@ -176,7 +181,8 @@ st_end_input (SCM port, int offset) if (pt->read_pos - pt->read_buf < offset) scm_misc_error ("st_end_input", "negative position", SCM_EOL); - pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset); + pt->read_pos -= offset; + pt->write_pos = (unsigned char *) pt->read_pos; pt->rw_active = SCM_PORT_NEITHER; } @@ -202,6 +208,8 @@ st_seek (SCM port, scm_t_off offset, int whence) else /* all other cases. */ { + scm_t_off base = 0; + if (pt->rw_active == SCM_PORT_WRITE) st_flush (port); @@ -211,18 +219,24 @@ st_seek (SCM port, scm_t_off offset, int whence) switch (whence) { case SEEK_CUR: - target = pt->read_pos - pt->read_buf + offset; + base = pt->read_pos - pt->read_buf; break; case SEEK_END: - target = pt->read_end - pt->read_buf + offset; + base = pt->read_end - pt->read_buf; break; - default: /* SEEK_SET */ - target = offset; + case SEEK_SET: + base = 0; break; + default: + scm_wrong_type_arg_msg ("st_seek", 0, port, + "invalid `whence' argument"); } + if (offset > SCM_T_OFF_MAX - base) + scm_misc_error ("st_seek", "target would overflow", SCM_EOL); + target = base + offset; if (target < 0) - scm_misc_error ("st_seek", "negative offset", SCM_EOL); + scm_misc_error ("st_seek", "negative target", SCM_EOL); if (target >= pt->write_buf_size) { @@ -235,7 +249,9 @@ st_seek (SCM port, scm_t_off offset, int whence) SCM_EOL); } } - else if (target == pt->write_buf_size) + else if (target > SCM_T_OFF_MAX - target) + scm_misc_error ("st_seek", "target * 2 would overflow", SCM_EOL); + else st_resize_port (pt, target * 2); } pt->read_pos = pt->write_pos = pt->read_buf + target; -- 2.5.0 --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Wed Nov 04 16:13:08 2015 Received: (at 20302-done) by debbugs.gnu.org; 4 Nov 2015 21:13:08 +0000 Received: from localhost ([127.0.0.1]:53744 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Zu5Mh-000755-Ld for submit@debbugs.gnu.org; Wed, 04 Nov 2015 16:13:07 -0500 Received: from world.peace.net ([50.252.239.5]:57722) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Zu5MO-00074P-2J for 20302-done@debbugs.gnu.org; Wed, 04 Nov 2015 16:13:06 -0500 Received: from [10.1.10.104] (helo=jojen) by world.peace.net with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1Zu5MG-0002F8-5u; Wed, 04 Nov 2015 16:12:40 -0500 From: Mark H Weaver To: David Kastrup Subject: Re: bug#20302: peek-char messes up file position on binary string ports References: <87y4lylwch.fsf@fencepost.gnu.org> <87383z5nmj.fsf@netris.org> <87oahfoj8p.fsf@netris.org> Date: Wed, 04 Nov 2015 16:12:38 -0500 In-Reply-To: <87oahfoj8p.fsf@netris.org> (Mark H. Weaver's message of "Sun, 06 Sep 2015 07:55:02 -0400") Message-ID: <87k2pxzcxl.fsf@netris.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 20302-done Cc: 20302-done@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 0.0 (/) Mark H Weaver writes: > Mark H Weaver writes: > >> David Kastrup 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). > > I've attached a preliminary patch set to fix this bug and some others. I believe this is now fixed on the stable-2.0 branch, commit 448eb30e3d9e998e97a5d51875f861c9f6c1101c. I'm closing this bug now. Thanks, Mark From unknown Tue Aug 19 02:51:03 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Thu, 03 Dec 2015 12:24:04 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator