Skip to content

Commit c2bdc4c

Browse files
tooonifabpot
authored andcommitted
[Messenger] Added check if json_encode succeeded
1 parent f3d8fd2 commit c2bdc4c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ public function testGetNonBlocking()
177177
$redis->del('messenger-getnonblocking');
178178
}
179179

180+
public function testJsonError()
181+
{
182+
$redis = new \Redis();
183+
$connection = Connection::fromDsn('redis://localhost/json-error', [], $redis);
184+
try {
185+
$connection->add("\xB1\x31", []);
186+
} catch (TransportException $e) {
187+
}
188+
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', $e->getMessage());
189+
}
190+
180191
public function testLastErrorGetsCleared()
181192
{
182193
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();

src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,16 @@ public function add(string $body, array $headers): void
185185
}
186186

187187
try {
188-
$added = $this->connection->xadd($this->stream, '*', ['message' => json_encode(
189-
['body' => $body, 'headers' => $headers]
190-
)]);
188+
$message = json_encode([
189+
'body' => $body,
190+
'headers' => $headers,
191+
]);
192+
193+
if (false === $message) {
194+
throw new TransportException(json_last_error_msg());
195+
}
196+
197+
$added = $this->connection->xadd($this->stream, '*', ['message' => $message]);
191198
} catch (\RedisException $e) {
192199
throw new TransportException($e->getMessage(), 0, $e);
193200
}

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