Skip to content

[Mime] Fix memory leak #51874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Symfony/Component/Mime/RawMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public function toString(): string
if (\is_string($this->message)) {
return $this->message;
}
if ($this->message instanceof \Traversable) {
$this->message = iterator_to_array($this->message, false);

$message = '';
foreach ($this->message as $chunk) {
$message .= $chunk;
}

return $this->message = implode('', $this->message);
return $this->message = $message;
}

public function toIterable(): iterable
Expand All @@ -45,12 +47,9 @@ public function toIterable(): iterable
return;
}

$message = '';
foreach ($this->message as $chunk) {
$message .= $chunk;
yield $chunk;
}
$this->message = $message;
}

/**
Expand Down
46 changes: 25 additions & 21 deletions src/Symfony/Component/Mime/Tests/RawMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,40 @@ class RawMessageTest extends TestCase
/**
* @dataProvider provideMessages
*/
public function testToString($messageParameter)
public function testToString(mixed $messageParameter, bool $supportReuse)
{
$message = new RawMessage($messageParameter);
$this->assertEquals('some string', $message->toString());
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
// calling methods more than once work
$this->assertEquals('some string', $message->toString());
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));

if ($supportReuse) {
// calling methods more than once work
$this->assertEquals('some string', $message->toString());
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
}
}

public static function provideMessages(): array
/**
* @dataProvider provideMessages
*/
public function testSerialization(mixed $messageParameter, bool $supportReuse)
{
return [
'string' => ['some string'],
'traversable' => [new \ArrayObject(['some', ' ', 'string'])],
'array' => [['some', ' ', 'string']],
];
$message = new RawMessage($messageParameter);
$this->assertEquals('some string', unserialize(serialize($message))->toString());

if ($supportReuse) {
// calling methods more than once work
$this->assertEquals('some string', unserialize(serialize($message))->toString());
}
}

public function testSerialization()
public static function provideMessages(): array
{
$message = new RawMessage('string');
$this->assertEquals('string', unserialize(serialize($message))->toString());
// calling methods more than once work
$this->assertEquals('string', unserialize(serialize($message))->toString());

$message = new RawMessage(new \ArrayObject(['some', ' ', 'string']));
$message = new RawMessage($message->toIterable());
$this->assertEquals('some string', unserialize(serialize($message))->toString());
// calling methods more than once work
$this->assertEquals('some string', unserialize(serialize($message))->toString());
return [
'string' => ['some string', true],
'traversable' => [new \ArrayObject(['some', ' ', 'string']), true],
'array' => [['some', ' ', 'string'], true],
'generator' => [(function () { yield 'some'; yield ' '; yield 'string'; })(), false],
];
}
}