]> Sergey Matveev's repositories - public-inbox.git/commitdiff
spawnpp: raise exception on E2BIG errors
authorEric Wong <e@80x24.org>
Mon, 8 Feb 2021 09:05:21 +0000 (23:05 -1000)
committerEric Wong <e@80x24.org>
Mon, 8 Feb 2021 22:07:50 +0000 (22:07 +0000)
This matches the Inline::C version, and lets us test
argv overflow with $search->query_argv_to_string;

lib/PublicInbox/SpawnPP.pm
t/search.t

index 2c5edef62efc55acbf48e2e0f4cc6efa6ac852d2..6d7e2c34ec9046111056b350084c947dbaa070e0 100644 (file)
@@ -16,13 +16,19 @@ sub pi_fork_exec ($$$$$$$) {
        $set->fillset or die "fillset failed: $!";
        sigprocmask(SIG_SETMASK, $set, $old) or die "can't block signals: $!";
        my $syserr;
+       pipe(my ($r, $w));
        my $pid = fork;
        unless (defined $pid) { # compat with Inline::C version
                $syserr = $!;
                $pid = -1;
        }
        if ($pid == 0) {
-               $SIG{__DIE__} = sub { warn @_; _exit 1 };
+               close $r;
+               $SIG{__DIE__} = sub {
+                       warn(@_);
+                       syswrite($w, my $num = $! + 0);
+                       _exit(1);
+               };
                for my $child_fd (0..$#$redir) {
                        my $parent_fd = $redir->[$child_fd];
                        next if $parent_fd == $child_fd;
@@ -32,7 +38,9 @@ sub pi_fork_exec ($$$$$$$) {
                if ($pgid >= 0 && !defined(setpgid(0, $pgid))) {
                        die "setpgid(0, $pgid): $!";
                }
-               $SIG{$_} = 'DEFAULT' for keys %SIG;
+               for (keys %SIG) {
+                       $SIG{$_} = 'DEFAULT' if substr($_, 0, 1) ne '_';
+               }
                if ($cd ne '') {
                        chdir $cd or die "chdir $cd: $!";
                }
@@ -49,11 +57,18 @@ sub pi_fork_exec ($$$$$$$) {
                } else {
                        %ENV = map { split(/=/, $_, 2) } @$env;
                }
-               exec @$cmd;
+               undef $r;
+               exec { $f } @$cmd;
                die "exec @$cmd failed: $!";
        }
+       close $w;
        sigprocmask(SIG_SETMASK, $old) or die "can't unblock signals: $!";
-       $! = $syserr;
+       if (my $cerrnum = do { local $/, <$r> }) {
+               $pid = -1;
+               $! = $cerrnum;
+       } else {
+               $! = $syserr;
+       }
        $pid;
 }
 
index 56c7db1c88fd9c2c6eab8ceedfef3e57bc353502..36a8fb3085584a2342248ed29e32ff0bd7eb84f8 100644 (file)
@@ -576,6 +576,13 @@ SKIP: {
        $q = $s->query_argv_to_string($g, [qw{OR (rt:1993-10-02)}]);
        like($q, qr/\AOR \(rt:749\d{6}\.\.749\d{6}\)\z/,
                'trailing parentheses preserved');
+       $ENV{TEST_EXPENSIVE} or
+               skip 'TEST_EXPENSIVE not set for argv overflow check', 1;
+       my @w;
+       local $SIG{__WARN__} = sub { push @w, @_ }; # for pure Perl version
+       my @fail = map { 'd:1993-10-02..2010-10-02' } (1..(4096 * 32));
+       eval { $s->query_argv_to_string($g, \@fail) };
+       ok($@, 'exception raised');
 }
 
 done_testing();