Skip to content

Commit

Permalink
Remove stream filters only if they are still valid (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
crocodele authored Dec 18, 2024
1 parent 7d1517d commit b6dfea1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace League\Csv;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Group;
use SplFileObject;
use SplTempFileObject;
Expand Down Expand Up @@ -637,4 +638,18 @@ public function testHeaderMapperFailsWithInvalidMapper(): void
Reader::createFromString($csv)
->getRecords(['Annee' => 'Year', 'Prenom' => 'Firstname', 'Nombre' => 'Count']);
}

#[DoesNotPerformAssertions]
public function testStreamWithFiltersDestructsGracefully(): void
{
/** @var resource $fp */
$fp = fopen('php://temp', 'r+');
fputcsv($fp, ['abc', '123'], escape: '');

$csv = Reader::createFromStream($fp);
$csv->addStreamFilter('convert.iconv.UTF-8/UTF-16');

// An explicitly closed file handle makes the stream filter resources invalid
fclose($fp);
}
}
6 changes: 5 additions & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ private function __construct($stream)

public function __destruct()
{
array_walk_recursive($this->filters, fn ($filter): bool => @stream_filter_remove($filter));
array_walk_recursive($this->filters, static function ($filter): void {
if (is_resource($filter)) {
@stream_filter_remove($filter);
}
});

if ($this->should_close_stream) {
set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true);
Expand Down

0 comments on commit b6dfea1

Please sign in to comment.