Skip to content

Commit 2df9e70

Browse files
committed
Fix possible memory leak #563
1 parent 3f11e12 commit 2df9e70

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
All Notable changes to `Csv` will be documented in this file
44

5+
## [Next](https://github.com/thephpleague/csv/compare/9.24.0...master) - Next
6+
7+
### Added
8+
9+
- None
10+
11+
### Deprecated
12+
13+
- None
14+
15+
### Fixed
16+
17+
- Fix possible memory leaks with the `Writer` class [#563](https://github.com/thephpleague/csv/issues/563) - thanks to [pope-12](https://github.com/pope-12)
18+
19+
### Remove
20+
21+
- None
22+
523
## [9.24.0](https://github.com/thephpleague/csv/compare/9.23.0...9.24.0) - 2025-06-24
624

725
### Added

src/Writer.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class Writer extends AbstractCsv implements TabularDataWriter
4343
protected int $enclose_all = self::ENCLOSE_NECESSARY;
4444
/** @var array{0:array<string>,1:array<string>} */
4545
protected array $enclosure_replace = [[], []];
46-
/** @var Closure(array): (int|false) */
47-
protected Closure $insertRecord;
4846

4947
protected function resetProperties(): void
5048
{
@@ -54,17 +52,20 @@ protected function resetProperties(): void
5452
[$this->enclosure, $this->escape.$this->enclosure.$this->enclosure],
5553
[$this->enclosure.$this->enclosure, $this->escape.$this->enclosure],
5654
];
55+
}
5756

58-
$this->insertRecord = match ($this->enclose_all) {
59-
self::ENCLOSE_ALL => fn (array $record): int|false => $this->document->fwrite(implode(
57+
protected function insertRecord(array $record): int|false
58+
{
59+
return match ($this->enclose_all) {
60+
self::ENCLOSE_ALL => $this->document->fwrite(implode(
6061
$this->delimiter,
6162
array_map(
6263
fn ($content) => $this->enclosure.$content.$this->enclosure,
6364
str_replace($this->enclosure_replace[0], $this->enclosure_replace[1], $record)
6465
)
6566
).$this->newline),
66-
self::ENCLOSE_NONE => fn (array $record): int|false => $this->document->fwrite(implode($this->delimiter, $record).$this->newline),
67-
default => fn (array $record): int|false => $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape, $this->newline),
67+
self::ENCLOSE_NONE => $this->document->fwrite(implode($this->delimiter, $record).$this->newline),
68+
default => $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape, $this->newline),
6869
};
6970
}
7071

@@ -143,7 +144,7 @@ public function insertOne(array $record): int
143144
$record = array_reduce($this->formatters, fn (array $record, callable $formatter): array => $formatter($record), $record);
144145
$this->validateRecord($record);
145146
set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true);
146-
$bytes = ($this->insertRecord)($record);
147+
$bytes = $this->insertRecord($record);
147148
restore_error_handler();
148149
if (false === $bytes) {
149150
throw CannotInsertRecord::triggerOnInsertion($record);

0 commit comments

Comments
 (0)