Skip to content

Commit

Permalink
Fix serializer to account for non-UTF-8 chars (port of #553 to 2.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed May 4, 2018
1 parent add68f2 commit 477d0c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/Raven/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,22 @@ public function serializeObject($object, $max_depth = 3, $_depth = 0, $hashes =
protected function serializeString($value)
{
$value = (string) $value;
if (function_exists('mb_detect_encoding')
&& function_exists('mb_convert_encoding')
) {

if (extension_loaded('mbstring')) {
// we always guarantee this is coerced, even if we can't detect encoding
if ($currentEncoding = mb_detect_encoding($value, $this->mb_detect_order)) {
$value = mb_convert_encoding($value, 'UTF-8', $currentEncoding);
} else {
$value = mb_convert_encoding($value, 'UTF-8');
}
}

if (strlen($value) > $this->messageLimit) {
$value = substr($value, 0, $this->messageLimit - 10) . ' {clipped}';
if (mb_strlen($value) > $this->messageLimit) {
$value = mb_substr($value, 0, $this->messageLimit - 10, 'UTF-8') . ' {clipped}';
}
} else {
if (strlen($value) > $this->messageLimit) {
$value = substr($value, 0, $this->messageLimit - 10) . ' {clipped}';
}
}

return $value;
Expand Down
18 changes: 18 additions & 0 deletions tests/SerializerAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,24 @@ public function testSetAllObjectSerialize()
$serializer->setAllObjectSerialize(false);
$this->assertFalse($serializer->getAllObjectSerialize());
}

public function testClippingUTF8Characters()
{
if (!extension_loaded('mbstring')) {
$this->markTestSkipped('mbstring extension is not enabled.');
}

$testString = 'Прекратите надеяться, что ваши пользователи будут сообщать об ошибках';
$class_name = static::get_test_class();
/** @var \Raven\Serializer $serializer */
$serializer = new $class_name(null, 19);

$clipped = $serializer->serialize($testString);

$this->assertEquals('Прекратит {clipped}', $clipped);
$this->assertNotNull(json_encode($clipped));
$this->assertSame(JSON_ERROR_NONE, json_last_error());
}
}

/**
Expand Down

0 comments on commit 477d0c9

Please sign in to comment.