From: Frank Lichtenheld Date: Fri, 15 Feb 2008 21:58:26 +0000 (+0100) Subject: Dpkg::IPC: Add a sanitiy_check for options of fork_and_exec X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8809ecced7e0972959891426fb2a9fbf893d3a74;p=dpkg Dpkg::IPC: Add a sanitiy_check for options of fork_and_exec * scripts/Dpkg/IPC.pm (_sanity_check_opts): Check for some probable errors in options. (fork_and_exec): Apply _sanity_check_opts to the options to catch some errors that might go unnoticed otherwise and to error out early for some other errors. --- diff --git a/scripts/Dpkg/IPC.pm b/scripts/Dpkg/IPC.pm index cbc1ad0a..ee7c2c15 100644 --- a/scripts/Dpkg/IPC.pm +++ b/scripts/Dpkg/IPC.pm @@ -105,10 +105,35 @@ but not the pid. =cut -sub fork_and_exec { +sub _sanity_check_opts { my (%opts) = @_; + + error("exec parameter is mandatory in fork_and_exec()") + unless $opts{"exec"}; + + my $to = my $from = 0; + foreach (qw(file handle string pipe)) { + $to++ if $opts{"to_$_"}; + $from++ if $opts{"from_$_"}; + } + error("not more than one of to_* parameters is allowed") + if $to > 1; + error("not more than one of from_* parameters is allowed") + if $from > 1; + + foreach (qw(to_string from_string to_pipe from_pipe)) { + if (exists $opts{$_} and + (!ref($opts{$_}) or ref($opts{$_}) ne 'SCALAR')) { + error("paramter $_ must be a scalar reference"); + } + } + + return %opts; +} + +sub fork_and_exec { + my (%opts) = _sanity_check_opts(@_); $opts{"close_in_child"} ||= []; - error("exec parameter is mandatory in fork_and_exec()") unless $opts{"exec"}; my @prog; if (ref($opts{"exec"}) =~ /ARRAY/) { push @prog, @{$opts{"exec"}};