user/dev discussion of public-inbox itself
 help / Atom feed
* [PATCH] avoid IO::File for anonymous temporary files
@ 2016-11-26  8:55 Eric Wong
  2016-12-25  9:40 ` [PATCH 2/1] http: fix clobbering of $null_io Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2016-11-26  8:55 UTC (permalink / raw)
  To: meta

We do not need to import IO::File into the main programs
since Perl 5.8+ supports literal "undef" for generating
anonymous temporary file handles.
---
 lib/PublicInbox/GitHTTPBackend.pm  | 4 ++--
 lib/PublicInbox/HTTP.pm            | 8 ++++----
 lib/PublicInbox/Spamcheck/Spamc.pm | 5 ++---
 t/httpd-corner.t                   | 3 +--
 4 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm
index 322005b..1987a01 100644
--- a/lib/PublicInbox/GitHTTPBackend.pm
+++ b/lib/PublicInbox/GitHTTPBackend.pm
@@ -7,7 +7,7 @@ package PublicInbox::GitHTTPBackend;
 use strict;
 use warnings;
 use Fcntl qw(:seek);
-use IO::File;
+use IO::Handle;
 use HTTP::Date qw(time2str);
 use HTTP::Status qw(status_message);
 use Plack::Util;
@@ -272,7 +272,7 @@ sub serve_smart {
 
 sub input_to_file {
 	my ($env) = @_;
-	my $in = IO::File->new_tmpfile;
+	open(my $in, '+>', undef);
 	unless (defined $in) {
 		err($env, "could not open temporary file: $!");
 		return;
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 729d46f..cac14be 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -17,7 +17,7 @@ use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
 use Scalar::Util qw(weaken);
-use IO::File;
+use IO::Handle;
 use constant {
 	CHUNK_START => -1,   # [a-f0-9]+\r\n
 	CHUNK_END => -2,     # \r\n
@@ -43,7 +43,7 @@ sub process_pipelineq () {
 our $MAX_REQUEST_BUFFER = $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} ||
 			(10 * 1024 * 1024);
 
-my $null_io = IO::File->new('/dev/null', '<');
+open(my $null_io, '<', '/dev/null') or die "failed to open /dev/null: $!";
 my $http_date;
 my $prev = 0;
 sub http_date () {
@@ -335,10 +335,10 @@ sub input_prepare {
 			quit($self, 413);
 			return;
 		}
-		$input = IO::File->new_tmpfile;
+		open($input, '+>', undef);
 	} elsif (env_chunked($env)) {
 		$len = CHUNK_START;
-		$input = IO::File->new_tmpfile;
+		open($input, '+>', undef);
 	}
 
 	# TODO: expire idle clients on ENFILE / EMFILE
diff --git a/lib/PublicInbox/Spamcheck/Spamc.pm b/lib/PublicInbox/Spamcheck/Spamc.pm
index 5190c26..30eec95 100644
--- a/lib/PublicInbox/Spamcheck/Spamc.pm
+++ b/lib/PublicInbox/Spamcheck/Spamc.pm
@@ -4,7 +4,7 @@ package PublicInbox::Spamcheck::Spamc;
 use strict;
 use warnings;
 use PublicInbox::Spawn qw(popen_rd spawn);
-use IO::File;
+use IO::Handle;
 use Fcntl qw(:DEFAULT SEEK_SET);
 
 sub new {
@@ -72,13 +72,12 @@ sub _devnull {
 
 sub _msg_to_fd {
 	my ($self, $msg, $tmpref) = @_;
-	my $tmpfh;
 	my $fd;
 	if (my $ref = ref($msg)) {
 		my $fileno = eval { fileno($msg) };
 		return $fileno if defined $fileno;
 
-		$tmpfh = IO::File->new_tmpfile;
+		open(my $tmpfh, '+>', undef) or die "failed to open: $!";
 		$tmpfh->autoflush(1);
 		$msg = \($msg->as_string) if $ref ne 'SCALAR';
 		print $tmpfh $$msg or die "failed to print: $!";
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 1e8465c..8a0337c 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -243,7 +243,6 @@ my $check_self = sub {
 
 SKIP: {
 	use POSIX qw(dup2);
-	use IO::File;
 	my $have_curl = 0;
 	foreach my $p (split(':', $ENV{PATH})) {
 		-x "$p/curl" or next;
@@ -255,7 +254,7 @@ SKIP: {
 	my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
 	my ($r, $w);
 	pipe($r, $w) or die "pipe: $!";
-	my $tout = IO::File->new_tmpfile or die "new_tmpfile: $!";
+	open(my $tout, '+>', undef) or die "open temporary file: $!";
 	my $pid = fork;
 	defined $pid or die "fork: $!";
 	my @cmd = (qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url);
-- 
EW


^ permalink raw reply	[flat|threaded] 2+ messages in thread

* [PATCH 2/1] http: fix clobbering of $null_io
  2016-11-26  8:55 [PATCH] avoid IO::File for anonymous temporary files Eric Wong
@ 2016-12-25  9:40 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-12-25  9:40 UTC (permalink / raw)
  To: meta

Oops, this would be disatrous if we started handling
bigger request bodies or slow clients.

Fixes: c008654229a9 ("avoid IO::File for anonymous temporary files")
---
 lib/PublicInbox/HTTP.pm | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index cac14be..c4b74b4 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -328,7 +328,7 @@ sub more ($$) {
 
 sub input_prepare {
 	my ($self, $env) = @_;
-	my $input = $null_io;
+	my $input;
 	my $len = $env->{CONTENT_LENGTH};
 	if ($len) {
 		if ($len > $MAX_REQUEST_BUFFER) {
@@ -339,6 +339,8 @@ sub input_prepare {
 	} elsif (env_chunked($env)) {
 		$len = CHUNK_START;
 		open($input, '+>', undef);
+	} else {
+		$input = $null_io;
 	}
 
 	# TODO: expire idle clients on ENFILE / EMFILE
-- 
EW


^ permalink raw reply	[flat|threaded] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-26  8:55 [PATCH] avoid IO::File for anonymous temporary files Eric Wong
2016-12-25  9:40 ` [PATCH 2/1] http: fix clobbering of $null_io Eric Wong

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.org/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/
       or Tor2web: https://www.tor2web.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox