From 29e530f1c0694107ad25881a2c693c30783f85f1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 7 Feb 2021 23:05:21 -1000 Subject: spawnpp: raise exception on E2BIG errors This matches the Inline::C version, and lets us test argv overflow with $search->query_argv_to_string; --- lib/PublicInbox/SpawnPP.pm | 23 +++++++++++++++++++---- t/search.t | 7 +++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/PublicInbox/SpawnPP.pm b/lib/PublicInbox/SpawnPP.pm index 2c5edef6..6d7e2c34 100644 --- a/lib/PublicInbox/SpawnPP.pm +++ b/lib/PublicInbox/SpawnPP.pm @@ -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; } diff --git a/t/search.t b/t/search.t index 56c7db1c..36a8fb30 100644 --- a/t/search.t +++ b/t/search.t @@ -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(); -- cgit v1.2.3-24-ge0c7