From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Andy Wingo Newsgroups: gmane.lisp.guile.bugs Subject: bug#30066: 'get-bytevector-some' returns only 1 byte from unbuffered ports Date: Wed, 10 Jan 2018 17:32:04 +0100 Message-ID: <87fu7dptdn.fsf@igalia.com> References: <87zi5lrc3x.fsf@gnu.org> <87tvvtr9ge.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1515601885 30947 195.159.176.226 (10 Jan 2018 16:31:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 10 Jan 2018 16:31:25 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: 30066@debbugs.gnu.org To: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Jan 10 17:31:20 2018 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZJHU-0007B5-8X for guile-bugs@m.gmane.org; Wed, 10 Jan 2018 17:31:12 +0100 Original-Received: from localhost ([::1]:52372 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZJJT-00073L-MA for guile-bugs@m.gmane.org; Wed, 10 Jan 2018 11:33:15 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZJJJ-000714-98 for bug-guile@gnu.org; Wed, 10 Jan 2018 11:33:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZJJG-0004Aq-4y for bug-guile@gnu.org; Wed, 10 Jan 2018 11:33:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:43320) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eZJJG-0004Ae-0m for bug-guile@gnu.org; Wed, 10 Jan 2018 11:33:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eZJJF-0004co-Md for bug-guile@gnu.org; Wed, 10 Jan 2018 11:33:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Andy Wingo Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 10 Jan 2018 16:33:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30066 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 30066-submit@debbugs.gnu.org id=B30066.151560193417662 (code B ref 30066); Wed, 10 Jan 2018 16:33:01 +0000 Original-Received: (at 30066) by debbugs.gnu.org; 10 Jan 2018 16:32:14 +0000 Original-Received: from localhost ([127.0.0.1]:51215 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZJIU-0004an-G0 for submit@debbugs.gnu.org; Wed, 10 Jan 2018 11:32:14 -0500 Original-Received: from pb-sasl2.pobox.com ([64.147.108.67]:51125 helo=sasl.smtp.pobox.com) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZJIT-0004ad-3u for 30066@debbugs.gnu.org; Wed, 10 Jan 2018 11:32:13 -0500 Original-Received: from sasl.smtp.pobox.com (unknown [127.0.0.1]) by pb-sasl2.pobox.com (Postfix) with ESMTP id 40832C0626; Wed, 10 Jan 2018 11:32:12 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type:content-transfer-encoding; s=sasl; bh=kSiE7kI4R3VS 3RaHrdw1RM+ft8c=; b=rinpMSt3L/oqdUB8ksAj1r+dP59NOyDbgyESMXSaSauI HrSIy5hC7f1Zi+FxJnzUMvNj2O5Ru8u2flF3/cb+xgetQSBhYQwPjTnGsUX+ZvYQ p4QcD7IRtoHuyVLAdRWcDaCcjMYqAZFgtpKN6qdxEYsbvdZI8pyF5p7NipLgrIA= Original-Received: from pb-sasl2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-sasl2.pobox.com (Postfix) with ESMTP id 2915DC0624; Wed, 10 Jan 2018 11:32:12 -0500 (EST) Original-Received: from rusty (unknown [88.160.190.192]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by pb-sasl2.pobox.com (Postfix) with ESMTPSA id 170C1C0623; Wed, 10 Jan 2018 11:32:10 -0500 (EST) In-Reply-To: <87tvvtr9ge.fsf@gnu.org> ("Ludovic =?UTF-8?Q?Court=C3=A8s?="'s message of "Wed, 10 Jan 2018 16:59:29 +0100") X-Pobox-Relay-ID: CED2A196-F623-11E7-BD1C-EA54894C8D7C-02397024!pb-sasl2.pobox.com X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:8953 Archived-At: On Wed 10 Jan 2018 16:59, ludo@gnu.org (Ludovic Court=C3=A8s) writes: > ludo@gnu.org (Ludovic Court=C3=A8s) skribis: > >> As discussed on IRC, =E2=80=98get-bytevector-some=E2=80=99 returns only = 1 byte from >> unbuffered ports: > > Here=E2=80=99s a tentative fix. WDYT? Thanks! Needs a little work though :) Comments inline. > --- a/libguile/ports.h > +++ b/libguile/ports.h > @@ -69,6 +69,7 @@ SCM_INTERNAL SCM scm_i_port_weak_set; > #define SCM_OPOUTPORTP(x) (SCM_OPPORTP (x) && SCM_OUTPUT_PORT_P (x)) > #define SCM_OPENP(x) (SCM_OPPORTP (x)) > #define SCM_CLOSEDP(x) (!SCM_OPENP (x)) > +#define SCM_UNBUFFEREDP(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_= BUF0)) > #define SCM_CLR_PORT_OPEN_FLAG(p) \ > SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN) > #ifdef BUILDING_LIBGUILE Please guard this under #ifdef BUILDING_LIBGUILE. > @@ -487,16 +487,33 @@ SCM_DEFINE (scm_get_bytevector_some, "get-bytevecto= r-some", 1, 0, 0, >=20=20 > SCM_VALIDATE_BINARY_INPUT_PORT (1, port); >=20=20 > - buf =3D scm_fill_input (port, 0, &cur, &avail); > - if (avail =3D=3D 0) > + if (SCM_UNBUFFEREDP (port)) > { > - scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F); > - return SCM_EOF_VAL; > + size_t read; > + > + bv =3D scm_c_make_bytevector (4096); > + read =3D scm_i_read_bytes (port, bv, 0, SCM_BYTEVECTOR_LENGTH (bv)= ); > + > + if (read =3D=3D 0) > + return SCM_EOF_VAL; > + else if (read < SCM_BYTEVECTOR_LENGTH (bv)) > + return scm_c_shrink_bytevector (bv, read); > + else > + return bv; > } > + else > + { > + buf =3D scm_fill_input (port, 0, &cur, &avail); > + if (avail =3D=3D 0) > + { > + scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F); > + return SCM_EOF_VAL; > + } >=20=20 > - bv =3D scm_c_make_bytevector (avail); > - scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv= ), > - avail, cur, avail); > + bv =3D scm_c_make_bytevector (avail); > + scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS= (bv), > + avail, cur, avail); > + } >=20=20 > return bv; > } There are tabs in your code; would you mind doing only spaces? A port being unbuffered doesn't mean that it has no bytes in its buffer. In particular, scm_unget_bytes may put bytes back into the buffer. Or, peek-u8 might fill this buffer with one byte. Also, they port may have buffered write bytes (could be the port has write buffering but no read buffering). In that case (pt->rw_random) you need to scm_flush(). I suggest taking the buffered bytes from the read buffer, if any. Then if the port is unbuffered, make a bytevector and call scm_i_read_bytes; otherwise do the scm_fill_input path that's there already. One more thing, if the port goes EOF, you need to scm_port_buffer_set_has_eof_p. Regards, Andy