From f2ce8e498052351fd150b88ac69d53f6caab993c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 21 Sep 2026 20:18:29 +0100 Subject: [PATCH 1/3] Handle EINTR and read errors correctly _sysread_1 now encapsulates the two call sites of sysread. Functional changes here: * If we get a *read error* rather than EOF in parse_request or parse_headers, those functions return undef. That is correct, because otherwise we might process a partial request. * But if the error is EINTR, we ignore it and keep reading. Fixes https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1143215 --- lib/HTTP/Server/Simple.pm | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/HTTP/Server/Simple.pm b/lib/HTTP/Server/Simple.pm index 70fc38d..0f66c08 100644 --- a/lib/HTTP/Server/Simple.pm +++ b/lib/HTTP/Server/Simple.pm @@ -6,6 +6,7 @@ use FileHandle; use Socket; use Carp; use IO::Select; +use POSIX; use vars qw($VERSION $bad_request_doc); $VERSION = '0.52'; @@ -399,6 +400,19 @@ sub restart { exec {$0}( ( ( -x $0 ) ? () : ($^X) ), $0, @ARGV ); } +# Read 1 character from STDIN with sysread. Returns '' on EOF, or undef on error. +sub _sysread_1 { + my $buff; + for (;;) { + my $r = sysread( STDIN, $buff, 1 ); + return $buff // '' if defined $r; + # Normal perlio (read/print) handles EINTR for us. But we have to use sysread, + # which doesn't. We can get EINTR if we're in the middle of reading here, and + # handle a signal. As a library, we don't get to tell the application programmer + # they ought not to have any signal handlers. So we must retry on EINTR. + return undef if $! != EINTR; + } +} sub _process_request { my $self = shift; @@ -679,7 +693,7 @@ method, request URI and the protocol. sub parse_request { my $self = shift; my $chunk; - while ( sysread( STDIN, my $buff, 1 ) ) { + while ( length(my $buff = _sysread_1() // return undef) ) { last if $buff eq "\n"; $chunk .= $buff; } @@ -711,7 +725,7 @@ sub parse_headers { my @headers; my $chunk = ''; - while ( sysread( STDIN, my $buff, 1 ) ) { + while ( length(my $buff = _sysread_1() // return undef) ) { if ( $buff eq "\n" ) { $chunk =~ s/[\r\l\n\s]+$//; if ( $chunk =~ /^([^()<>\@,;:\\"\/\[\]?={} \t]+):\s*(.*)/i ) { -- 2.47.3