From 4c9117d3175578f94b18dca777e2055dc3e4b8e4 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 26 Jun 2023 14:05:53 +0200 Subject: [PATCH] Curl/Fsockopen: add some extra defensive coding The `error_get_last()` function can return `null` if no error occurred, in which case, the `throw new Exception()` statement will run into two new errors: * `Trying to access array offset on value of type null` for accessing `$error['message']`. * ... which then leads to a `Exception::__construct(): Passing null to parameter #1 ($message) of type string is deprecated`. This commit adds some defensive coding to handle the hypothetical situation that `error_get_last()` would return `null` when a file could not be opened. Note: this is actually a bug in PHPUnit 10 which breaks `error_get_last()`. We should be able to remove the extra defensive coding once the upstream bug has been fixed. Upstream bug report: https://github.com/sebastianbergmann/phpunit/issues/5428 --- src/Transport/Curl.php | 5 +++++ src/Transport/Fsockopen.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 0b3794808..3995197f4 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -177,6 +177,11 @@ public function request($url, $headers = [], $data = [], $options = []) { $this->stream_handle = @fopen($options['filename'], 'wb'); if ($this->stream_handle === false) { $error = error_get_last(); + if (!is_array($error)) { + // Shouldn't be possible, but can happen in test situations. + $error = ['message' => 'Failed to open stream']; + } + throw new Exception($error['message'], 'fopen'); } } diff --git a/src/Transport/Fsockopen.php b/src/Transport/Fsockopen.php index 97a1f7373..15a5ba9b0 100644 --- a/src/Transport/Fsockopen.php +++ b/src/Transport/Fsockopen.php @@ -282,6 +282,11 @@ public function request($url, $headers = [], $data = [], $options = []) { $download = @fopen($options['filename'], 'wb'); if ($download === false) { $error = error_get_last(); + if (!is_array($error)) { + // Shouldn't be possible, but can happen in test situations. + $error = ['message' => 'Failed to open stream']; + } + throw new Exception($error['message'], 'fopen'); } }