Skip to content

Commit 3fa982c

Browse files
committed
[Mime] Fix memory leak
1 parent 8dc9fee commit 3fa982c

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

src/Symfony/Component/Mime/RawMessage.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ public function __construct($message)
3030

3131
public function toString(): string
3232
{
33-
if (\is_string($this->message)) {
34-
return $this->message;
35-
}
36-
if ($this->message instanceof \Traversable) {
37-
$this->message = iterator_to_array($this->message, false);
33+
$str = '';
34+
foreach ($this->toIterable() as $chunk) {
35+
$str .= $chunk;
3836
}
3937

40-
return $this->message = implode('', $this->message);
38+
return $str;
4139
}
4240

4341
public function toIterable(): iterable
@@ -48,12 +46,19 @@ public function toIterable(): iterable
4846
return;
4947
}
5048

51-
$message = '';
52-
foreach ($this->message as $chunk) {
53-
$message .= $chunk;
54-
yield $chunk;
49+
if ($this->message instanceof \Generator) {
50+
$message = '';
51+
foreach ($this->message as $chunk) {
52+
$message .= $chunk;
53+
yield $chunk;
54+
}
55+
56+
$this->message = $message;
57+
} else {
58+
foreach ($this->message as $chunk) {
59+
yield $chunk;
60+
}
5561
}
56-
$this->message = $message;
5762
}
5863

5964
/**

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,24 @@ public function testToString($messageParameter)
2929
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
3030
}
3131

32+
/**
33+
* @dataProvider provideMessages
34+
*/
35+
public function testSerialization($messageParameter)
36+
{
37+
$message = new RawMessage($messageParameter);
38+
$this->assertEquals('some string', unserialize(serialize($message))->toString());
39+
// calling methods more than once work
40+
$this->assertEquals('some string', unserialize(serialize($message))->toString());
41+
}
42+
3243
public static function provideMessages(): array
3344
{
3445
return [
3546
'string' => ['some string'],
3647
'traversable' => [new \ArrayObject(['some', ' ', 'string'])],
3748
'array' => [['some', ' ', 'string']],
49+
'generator' => [(function () { yield 'some'; yield ' '; yield 'string'; })()],
3850
];
3951
}
40-
41-
public function testSerialization()
42-
{
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());
53-
}
5452
}

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