Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
0.08
- Fix Proc handles on Perl 5.8.
0.07
- Switch to using Capture::Tiny for Proc.pm, for Windows
support.
- Prevent a warning from Handle.pm during global destruction.
- Make Crypt::Random::Source work properly in taint mode.

0.06
- Use Any::Moose to allow either Moose or Mouse.
- Switch to dzil.
Expand Down
3 changes: 2 additions & 1 deletion dist.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = Crypt-Random-Source
version = 0.06
version = 0.08
author = Yuval Kogman <nothingmuch@woobling.org>
license = Perl_5
copyright_holder = Yuval Kogman
Expand All @@ -16,3 +16,4 @@ repository_at = github
[Prereqs]
Any::Moose = 0.11
namespace::clean = 0.08
Capture::Tiny = 0.08
6 changes: 4 additions & 2 deletions lib/Crypt/Random/Source/Base/Handle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ sub _read_too_short {
sub close {
my $self = shift;

if ( $self->has_handle ) {
$self->handle->close; # or die "close: $!"; # open "-|" returns exit status on close
# During global destruction, $self->handle can be undef already,
# so we need to also check if it is defined.
if ( $self->has_handle and $self->handle ) {
$self->handle->close or die "close: $!";
$self->clear_handle;
}
}
Expand Down
37 changes: 31 additions & 6 deletions lib/Crypt/Random/Source/Base/Proc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,47 @@ use Any::Moose;

extends qw(Crypt::Random::Source::Base::Handle);

use IO::Handle;
use Capture::Tiny qw(capture);
use File::Spec;
use IO::File;

use 5.008;

has command => ( is => "rw", required => 1 );
has search_path => ( is => 'rw', isa => 'Str', lazy_build => 1 );

# This is a scalar so that people can customize it (which they would
# particularly need to do on Windows).
our $TAINT_PATH =
'/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin';

sub _build_search_path {
# In taint mode it's not safe to use $ENV{PATH}.
if (${^TAINT}) {
return $TAINT_PATH;
}
return $ENV{PATH};
}

sub open_handle {
my $self = shift;

my $cmd = $self->command;
my @cmd = ref $cmd ? @$cmd : $cmd;

open my $fh, "-|", @cmd
or die "open(@cmd|): $!";

bless $fh, "IO::Handle";
my $retval;
local $ENV{PATH} = $self->search_path;
my ($stdout, $stderr) = capture { $retval = system(@cmd) };
chomp($stderr);
if ($retval) {
my $err = join(' ', @cmd) . ": $! ($?)";
if ($stderr) {
$err .= "\n$stderr";
}
die $err;
}
warn $stderr if $stderr;

open(my $fh, '<', \$stdout) || die $!;

return $fh;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Crypt/Random/Source/Factory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ sub first_available {

sub locate_sources {
my ( $self, $category ) = @_;
[ findsubmod "Crypt::Random::Source::$category" ];
my @sources = findsubmod "Crypt::Random::Source::$category";
# Untaint class names (which are tainted in taint mode because
# they came from the disk).
($_) = $_ =~ /^(.*)$/ foreach @sources;
return \@sources;
}

1;
Expand Down
7 changes: 3 additions & 4 deletions t/blocking.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More 'no_plan';
use Test::More tests => 3;

use 5.008;

Expand All @@ -12,9 +12,8 @@ use IO::Select;

use ok 'Crypt::Random::Source::Base::Handle';

{
my ( $reader, $writer ) = map { IO::Handle->new } 1 .. 2;

SKIP: {
skip "Windows can't open a blocking child pipe", 2 if $^O =~ /Win32/i;
defined ( my $child = open my $fh, "-|" ) or die "open: $!";

if ($child) {
Expand Down