Skip to content

Commit

Permalink
Curl/Fsockopen: add some extra defensive coding
Browse files Browse the repository at this point in the history
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: I actually suspect this is a bug in PHPUnit 10, but need to investigate further to be sure. In the mean time, our test shouldn't fail on it.
  • Loading branch information
jrfnl committed Jun 26, 2023
1 parent 436b4f0 commit 537413f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = array('message' => 'Failed to open stream');

Check failure on line 182 in src/Transport/Curl.php

View workflow job for this annotation

GitHub Actions / PHPCS

Short array syntax must be used to define arrays
}

throw new Exception($error['message'], 'fopen');
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Transport/Fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = array('message' => 'Failed to open stream');

Check failure on line 287 in src/Transport/Fsockopen.php

View workflow job for this annotation

GitHub Actions / PHPCS

Short array syntax must be used to define arrays
}

throw new Exception($error['message'], 'fopen');
}
}
Expand Down

0 comments on commit 537413f

Please sign in to comment.