From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E0CF51F8C8 for ; Thu, 26 Aug 2021 12:33:38 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 2/8] ds: use bytes::substr and bytes::length module-wide for now Date: Thu, 26 Aug 2021 12:33:32 +0000 Message-Id: <20210826123338.694-3-e@80x24.org> In-Reply-To: <20210826123338.694-1-e@80x24.org> References: <20210826123338.694-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: The use of substr within IO::Handle->write may not be correct if we have wide characters, so handle it ourselves. bytes.pm usage is probably better fixed in PublicInbox::NNTP, but the effort required is higher, so we'll just keep bytes in DS for now. --- lib/PublicInbox/DS.pm | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 7a4dfed0..d804792b 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -23,7 +23,7 @@ package PublicInbox::DS; use strict; use v5.10.1; use parent qw(Exporter); -use bytes; +use bytes qw(length substr); # FIXME(?): needed for PublicInbox::NNTP use POSIX qw(WNOHANG sigprocmask SIG_SETMASK); use Fcntl qw(SEEK_SET :DEFAULT O_APPEND); use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); @@ -499,13 +499,14 @@ sub drop { # n.b.: use ->write/->read for this buffer to allow compatibility with # PerlIO::mmap or PerlIO::scalar if needed sub tmpio ($$$) { - my ($self, $bref, $off) = @_; - my $fh = tmpfile('wbuf', $self->{sock}, O_APPEND) or - return drop($self, "tmpfile $!"); - $fh->autoflush(1); - my $len = bytes::length($$bref) - $off; - $fh->write($$bref, $len, $off) or return drop($self, "write ($len): $!"); - [ $fh, 0 ] # [1] = offset, [2] = length, not set by us + my ($self, $bref, $off) = @_; + my $fh = tmpfile('wbuf', $self->{sock}, O_APPEND) or + return drop($self, "tmpfile $!"); + $fh->autoflush(1); + my $len = length($$bref) - $off; + print $fh substr($$bref, $off, $len) or + return drop($self, "write ($len): $!"); + [ $fh, 0 ] # [1] = offset, [2] = length, not set by us } =head2 C<< $obj->write( $data ) >> @@ -547,7 +548,7 @@ sub write { $bref->($self); return 1; } else { - my $to_write = bytes::length($$bref); + my $to_write = length($$bref); my $written = syswrite($sock, $$bref, $to_write); if (defined $written) { @@ -582,7 +583,7 @@ sub msg_more ($$) { !$sock->can('stop_SSL')) { my $n = send($sock, $_[1], MSG_MORE); if (defined $n) { - my $nlen = bytes::length($_[1]) - $n; + my $nlen = length($_[1]) - $n; return 1 if $nlen == 0; # all done! # queue up the unwritten substring: my $tmpio = tmpio($self, \($_[1]), $n) or return 0;