Skip to content

Commit 100258f

Browse files
committed
Fix test for phpGH-10495: feof on OpenSSL stream hangs
1 parent c1bd9a9 commit 100258f

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

ext/openssl/tests/ServerClientTestCase.inc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,35 @@ function phpt_has_sslv3() {
2626
return $result;
2727
}
2828

29+
function phpt_extract_tls_records($rawData) {
30+
$records = [];
31+
$offset = 0;
32+
$dataLength = strlen($rawData);
33+
34+
while ($offset < $dataLength) {
35+
// Ensure there's enough data left for the header.
36+
if ($offset + 5 > $dataLength) {
37+
break;
38+
}
39+
40+
// Extract the length of the current record.
41+
$length = unpack("n", substr($rawData, $offset + 3, 2))[1];
42+
43+
// Check if the total length is within the bounds of the rawData.
44+
if ($offset + 5 + $length > $dataLength) {
45+
break;
46+
}
47+
48+
// Extract the record and add it to the records array.
49+
$records[] = substr($rawData, $offset, 5 + $length);
50+
51+
// Move the offset past the current record.
52+
$offset += 5 + $length;
53+
}
54+
55+
return $records;
56+
}
57+
2958
/**
3059
* This is a singleton to let the wait/notify functions work
3160
* I know it's horrible, but it's a means to an end

ext/openssl/tests/gh10495.phpt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,26 @@ $proxyCode = <<<'CODE'
6363
6464
$read = [$upstream, $conn];
6565
$applicationData = false;
66-
$i = 1;
6766
while (stream_select($read, $write, $except, 1)) {
6867
foreach ($read as $fp) {
6968
$data = stream_get_contents($fp);
7069
if ($fp === $conn) {
7170
fwrite($upstream, $data);
7271
} else {
73-
if ($data !== '' && $data[0] === chr(23)) {
74-
if (!$applicationData) {
75-
$applicationData = true;
76-
fwrite($conn, $data[0]);
77-
phpt_notify();
78-
sleep(1);
79-
fwrite($conn, substr($data, 1));
72+
foreach (phpt_extract_tls_records($data) as $record) {
73+
if ($record !== '' && $record[0] === chr(23)) {
74+
if (!$applicationData) {
75+
$applicationData = true;
76+
fwrite($conn, $record[0]);
77+
phpt_notify();
78+
sleep(1);
79+
fwrite($conn, substr($record, 1));
80+
} else {
81+
fwrite($conn, $record);
82+
}
8083
} else {
81-
fwrite($conn, $data);
84+
fwrite($conn, $record);
8285
}
83-
} else {
84-
fwrite($conn, $data);
8586
}
8687
}
8788
}

0 commit comments

Comments
 (0)