Skip to content

Commit d6c8797

Browse files
committed
bug #51874 [Mime] Fix memory leak (fabpot)
This PR was merged into the 6.4 branch. Discussion ---------- [Mime] Fix memory leak | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #51764 | License | MIT Commits ------- ee81ae4 [Mime] Fix memory leak
2 parents a0fdeaa + ee81ae4 commit d6c8797

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/Symfony/Component/Mime/RawMessage.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public function toString(): string
3030
if (\is_string($this->message)) {
3131
return $this->message;
3232
}
33-
if ($this->message instanceof \Traversable) {
34-
$this->message = iterator_to_array($this->message, false);
33+
34+
$message = '';
35+
foreach ($this->message as $chunk) {
36+
$message .= $chunk;
3537
}
3638

37-
return $this->message = implode('', $this->message);
39+
return $this->message = $message;
3840
}
3941

4042
public function toIterable(): iterable
@@ -45,12 +47,9 @@ public function toIterable(): iterable
4547
return;
4648
}
4749

48-
$message = '';
4950
foreach ($this->message as $chunk) {
50-
$message .= $chunk;
5151
yield $chunk;
5252
}
53-
$this->message = $message;
5453
}
5554

5655
/**

src/Symfony/Component/Mime/Tests/RawMessageTest.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,40 @@ class RawMessageTest extends TestCase
1919
/**
2020
* @dataProvider provideMessages
2121
*/
22-
public function testToString($messageParameter)
22+
public function testToString(mixed $messageParameter, bool $supportReuse)
2323
{
2424
$message = new RawMessage($messageParameter);
2525
$this->assertEquals('some string', $message->toString());
2626
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
27-
// calling methods more than once work
28-
$this->assertEquals('some string', $message->toString());
29-
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
27+
28+
if ($supportReuse) {
29+
// calling methods more than once work
30+
$this->assertEquals('some string', $message->toString());
31+
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
32+
}
3033
}
3134

32-
public static function provideMessages(): array
35+
/**
36+
* @dataProvider provideMessages
37+
*/
38+
public function testSerialization(mixed $messageParameter, bool $supportReuse)
3339
{
34-
return [
35-
'string' => ['some string'],
36-
'traversable' => [new \ArrayObject(['some', ' ', 'string'])],
37-
'array' => [['some', ' ', 'string']],
38-
];
40+
$message = new RawMessage($messageParameter);
41+
$this->assertEquals('some string', unserialize(serialize($message))->toString());
42+
43+
if ($supportReuse) {
44+
// calling methods more than once work
45+
$this->assertEquals('some string', unserialize(serialize($message))->toString());
46+
}
3947
}
4048

41-
public function testSerialization()
49+
public static function provideMessages(): array
4250
{
43-
$message = new RawMessage('string');
44-
$this->assertEquals('string', unserialize(serialize($message))->toString());
45-
// calling methods more than once work
46-
$this->assertEquals('string', unserialize(serialize($message))->toString());
47-
48-
$message = new RawMessage(new \ArrayObject(['some', ' ', 'string']));
49-
$message = new RawMessage($message->toIterable());
50-
$this->assertEquals('some string', unserialize(serialize($message))->toString());
51-
// calling methods more than once work
52-
$this->assertEquals('some string', unserialize(serialize($message))->toString());
51+
return [
52+
'string' => ['some string', true],
53+
'traversable' => [new \ArrayObject(['some', ' ', 'string']), true],
54+
'array' => [['some', ' ', 'string'], true],
55+
'generator' => [(function () { yield 'some'; yield ' '; yield 'string'; })(), false],
56+
];
5357
}
5458
}

0 commit comments

Comments
 (0)