| 1 |
From f2ce8e498052351fd150b88ac69d53f6caab993c Mon Sep 17 00:00:00 2001 |
| 2 |
From: Ian Jackson <[email protected]> |
| 3 |
Date: Mon, 21 Sep 2026 20:18:29 +0100 |
| 4 |
Subject: [PATCH 1/3] Handle EINTR and read errors correctly |
| 5 |
|
| 6 |
_sysread_1 now encapsulates the two call sites of sysread. |
| 7 |
|
| 8 |
Functional changes here: |
| 9 |
|
| 10 |
* If we get a *read error* rather than EOF in parse_request or |
| 11 |
parse_headers, those functions return undef. That is correct, |
| 12 |
because otherwise we might process a partial request. |
| 13 |
|
| 14 |
* But if the error is EINTR, we ignore it and keep reading. |
| 15 |
|
| 16 |
Fixes https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1143215 |
| 17 |
--- |
| 18 |
lib/HTTP/Server/Simple.pm | 18 ++++++++++++++++-- |
| 19 |
1 file changed, 16 insertions(+), 2 deletions(-) |
| 20 |
|
| 21 |
diff --git a/lib/HTTP/Server/Simple.pm b/lib/HTTP/Server/Simple.pm |
| 22 |
index 70fc38d..0f66c08 100644 |
| 23 |
--- a/lib/HTTP/Server/Simple.pm |
| 24 |
+++ b/lib/HTTP/Server/Simple.pm |
| 25 |
@@ -6,6 +6,7 @@ use FileHandle; |
| 26 |
use Socket; |
| 27 |
use Carp; |
| 28 |
use IO::Select; |
| 29 |
+use POSIX; |
| 30 |
|
| 31 |
use vars qw($VERSION $bad_request_doc); |
| 32 |
$VERSION = '0.52'; |
| 33 |
@@ -399,6 +400,19 @@ sub restart { |
| 34 |
exec {$0}( ( ( -x $0 ) ? () : ($^X) ), $0, @ARGV ); |
| 35 |
} |
| 36 |
|
| 37 |
+# Read 1 character from STDIN with sysread. Returns '' on EOF, or undef on error. |
| 38 |
+sub _sysread_1 { |
| 39 |
+ my $buff; |
| 40 |
+ for (;;) { |
| 41 |
+ my $r = sysread( STDIN, $buff, 1 ); |
| 42 |
+ return $buff // '' if defined $r; |
| 43 |
+ # Normal perlio (read/print) handles EINTR for us. But we have to use sysread, |
| 44 |
+ # which doesn't. We can get EINTR if we're in the middle of reading here, and |
| 45 |
+ # handle a signal. As a library, we don't get to tell the application programmer |
| 46 |
+ # they ought not to have any signal handlers. So we must retry on EINTR. |
| 47 |
+ return undef if $! != EINTR; |
| 48 |
+ } |
| 49 |
+} |
| 50 |
|
| 51 |
sub _process_request { |
| 52 |
my $self = shift; |
| 53 |
@@ -679,7 +693,7 @@ method, request URI and the protocol. |
| 54 |
sub parse_request { |
| 55 |
my $self = shift; |
| 56 |
my $chunk; |
| 57 |
- while ( sysread( STDIN, my $buff, 1 ) ) { |
| 58 |
+ while ( length(my $buff = _sysread_1() // return undef) ) { |
| 59 |
last if $buff eq "\n"; |
| 60 |
$chunk .= $buff; |
| 61 |
} |
| 62 |
@@ -711,7 +725,7 @@ sub parse_headers { |
| 63 |
my @headers; |
| 64 |
|
| 65 |
my $chunk = ''; |
| 66 |
- while ( sysread( STDIN, my $buff, 1 ) ) { |
| 67 |
+ while ( length(my $buff = _sysread_1() // return undef) ) { |
| 68 |
if ( $buff eq "\n" ) { |
| 69 |
$chunk =~ s/[\r\l\n\s]+$//; |
| 70 |
if ( $chunk =~ /^([^()<>\@,;:\\"\/\[\]?={} \t]+):\s*(.*)/i ) { |
| 71 |
-- |
| 72 |
2.47.3 |
| 73 |
|