]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
treewide: more autodie safety fixes for older Perl
authorEric Wong <e@80x24.org>
Wed, 15 Nov 2023 04:32:39 +0000 (04:32 +0000)
committerEric Wong <e@80x24.org>
Wed, 15 Nov 2023 08:02:57 +0000 (08:02 +0000)
Avoid mixing autodie use in different scopes since it's likely
to cause problems like it did in Gcf2.  While none of these
fix known problems with test cases, it's likely worthwhile to
avoid it anyways to avoid future surprises.

For Process::IO, we'll add some additional tests in t/io.t
to ensure we don't get unintended exceptions for try_cat.

lib/PublicInbox/IO.pm
lib/PublicInbox/LEI.pm
lib/PublicInbox/TestCommon.pm
lib/PublicInbox/XapHelperCxx.pm
t/extsearch.t
t/io.t

index 11ce9be1d4ccffa9a9c289c2e13ba1f5798837fe..63ae3ef4d78330866139bf9804ec7bcb0b5eb86b 100644 (file)
@@ -9,6 +9,8 @@ use PublicInbox::DS qw(awaitpid);
 our @EXPORT_OK = qw(poll_in read_all try_cat write_file);
 use Carp qw(croak);
 use IO::Poll qw(POLLIN);
+# don't autodie in top-level for Perl 5.16.3 (and maybe newer versions)
+# we have our own ->close, so we scope autodie into each sub
 
 sub waitcb { # awaitpid callback
        my ($pid, $errref, $cb, @args) = @_;
index 69065ce769424dca91b8008ab4fb7ba15b939936..460aed401c92fa5cbd8d9c5827105b0a5b60069a 100644 (file)
@@ -9,7 +9,7 @@ package PublicInbox::LEI;
 use v5.12;
 use parent qw(PublicInbox::DS PublicInbox::LeiExternal
        PublicInbox::LeiQuery);
-use autodie qw(bind chdir fork open socket socketpair syswrite unlink);
+use autodie qw(bind chdir fork open pipe socket socketpair syswrite unlink);
 use Getopt::Long ();
 use Socket qw(AF_UNIX SOCK_SEQPACKET pack_sockaddr_un);
 use Errno qw(EPIPE EAGAIN ECONNREFUSED ENOENT ECONNRESET);
@@ -1099,8 +1099,8 @@ sub start_pager {
        $new_env->{LESS} //= 'FRX';
        $new_env->{LV} //= '-c';
        $new_env->{MORE} = $new_env->{LESS} if $^O eq 'freebsd';
-       pipe(my ($r, $wpager)) or return warn "pipe: $!";
-       my $rdr = { 0 => $r, 1 => $self->{1}, 2 => $self->{2} };
+       my $rdr = { 1 => $self->{1}, 2 => $self->{2} };
+       CORE::pipe($rdr->{0}, my $wpager) or return warn "pipe: $!";
        my $pgr = [ undef, @$rdr{1, 2} ];
        my $env = $self->{env};
        if ($self->{sock}) { # lei(1) process runs it
@@ -1580,7 +1580,6 @@ sub slurp_stdin {
        my $in = $lei->{0};
        if (-t $in) { # run cat via script/lei and read from it
                $in = undef;
-               use autodie qw(pipe);
                pipe($in, my $wr);
                say { $lei->{2} } '# enter query, Ctrl-D when done';
                send_exec_cmd($lei, [ $lei->{0}, $wr ], ['cat'], {});
index a5546905f5113b55670fab6201676937342a4d6d..8bfa30f2669752bfd3cf594ee3e7a52cf06aa499 100644 (file)
@@ -16,7 +16,7 @@ our @EXPORT;
 my $lei_loud = $ENV{TEST_LEI_ERR_LOUD};
 my $tail_cmd = $ENV{TAIL};
 our ($lei_opt, $lei_out, $lei_err);
-use autodie qw(chdir close fcntl open opendir seek unlink);
+use autodie qw(chdir close fcntl mkdir open opendir seek unlink);
 
 $_ = File::Spec->rel2abs($_) for (grep(!m!^/!, @INC));
 
@@ -670,7 +670,6 @@ sub test_lei {
 SKIP: {
        my ($cb) = pop @_;
        my $test_opt = shift // {};
-       use autodie qw(mkdir);
        require_git(2.6, 1);
        my $mods = $test_opt->{mods} // [ 'lei' ];
        require_mods(@$mods, 2);
@@ -801,7 +800,7 @@ sub create_coderepo ($$;@) {
        my ($db) = (PublicInbox::Import::default_branch() =~ m!([^/]+)\z!);
        my $dir = "t/data-gen/$base.$ident-$db";
        my $new = !-d $dir;
-       if ($new && !mkdir($dir)) {
+       if ($new && !CORE::mkdir($dir)) {
                my $err = $!;
                -d $dir or xbail "mkdir($dir): $err";
        }
index e516b1118466445401279a29f0f535f61da02831..1250c9640213b0fe5beadf756ead6c4e351602a9 100644 (file)
@@ -8,10 +8,11 @@
 package PublicInbox::XapHelperCxx;
 use v5.12;
 use PublicInbox::Spawn qw(run_die run_qx which);
-use PublicInbox::IO qw(read_all write_file);
+use PublicInbox::IO qw(read_all try_cat write_file);
 use PublicInbox::Search;
 use Fcntl qw(SEEK_SET);
 use Config;
+use autodie;
 my $cxx = which($ENV{CXX} // 'c++');
 my $dir = substr("$cxx-$Config{archname}", 1); # drop leading '/'
 $dir =~ tr!/!-!;
@@ -39,25 +40,22 @@ EOM
 }
 
 sub needs_rebuild () {
-       open my $fh, '<', "$dir/XFLAGS" or return 1;
-       chomp(my $prev = read_all($fh));
+       my $prev = try_cat("$dir/XFLAGS") or return 1;
+       chomp $prev;
        return 1 if $prev ne $xflags;
 
-       open $fh, '<', "$dir/xap_modversion" or return 1;
-       chomp($prev = read_all($fh));
-       $prev or return 1;
+       $prev = try_cat("$dir/xap_modversion") or return 1;
+       chomp $prev;
 
        $xap_modversion = xap_cfg('--modversion');
        $xap_modversion ne $prev;
 }
 
 sub build () {
-       if (!-d $dir) {
-               my $err;
-               mkdir($dir) or $err = $!;
+       if (!-d $dir && !CORE::mkdir($dir)) {
+               my $err = $!;
                die "mkdir($dir): $err" if !-d $dir;
        }
-       use autodie;
        require PublicInbox::CodeSearch;
        require PublicInbox::Lock;
        require PublicInbox::OnDestroy;
index 1a1eb3500cd0cf5984977bdac7fad07079028769..090f6db5cc45eba90930f5c2a53b31b81cc0f7b7 100644 (file)
@@ -7,7 +7,7 @@ use PublicInbox::Config;
 use PublicInbox::InboxWritable;
 require_git(2.6);
 require_mods(qw(json DBD::SQLite Xapian));
-use autodie qw(open rename truncate);
+use autodie qw(open rename truncate unlink);
 require PublicInbox::Search;
 use_ok 'PublicInbox::ExtSearch';
 use_ok 'PublicInbox::ExtSearchIdx';
@@ -577,7 +577,6 @@ test_lei(sub {
        is_deeply($dst, \@md1,
                "convert from extindex w/ or w/o `extindex' prefix");
 
-       use autodie qw(unlink);
        my @o = glob "$home/extindex/ei*/over.sqlite*";
        unlink(@o);
        ok(!lei('convert', '-o', "$home/fail", "extindex:$d"));
diff --git a/t/io.t b/t/io.t
index 3ea00ed82e4fe1f1c631297b8762a2bfd5b432da..91f935263b77b0ad8f039258661599ab39d2d781 100644 (file)
--- a/t/io.t
+++ b/t/io.t
@@ -7,7 +7,7 @@ my $tmpdir = tmpdir;
 use_ok 'PublicInbox::IO';
 use PublicInbox::Spawn qw(which run_qx);
 
-# only test failures
+# test failures:
 SKIP: {
 my $strace = strace_inject;
 my $env = { PERL5LIB => join(':', @INC) };
@@ -24,4 +24,12 @@ like($err, qr/\bclose\b/, 'close error noted');
 is(-s $dst, 0, 'file created and empty after EIO');
 } # /SKIP
 
+PublicInbox::IO::write_file '>:unix', "$tmpdir/f", "HI\n";
+is(-s "$tmpdir/f", 3, 'write_file works w/ IO layer');
+PublicInbox::IO::write_file '>>', "$tmpdir/f", "HI\n";
+is(-s "$tmpdir/f", 6, 'write_file can append');
+
+is PublicInbox::IO::try_cat("$tmpdir/non-existent"), '',
+       "try_cat on non-existent file returns `'";
+
 done_testing;