Skip to content

Commit ee81ae4

Browse files
committed
[Mime] Fix memory leak
1 parent a0fdeaa commit ee81ae4

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy