Skip to content
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

Update Serializer.php #553

Merged
merged 10 commits into from
May 2, 2018
16 changes: 10 additions & 6 deletions lib/Raven/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,23 @@ public function serialize($value, $max_depth = 3, $_depth = 0)
protected function serializeString($value)
{
$value = (string) $value;
if (function_exists('mb_detect_encoding')
&& function_exists('mb_convert_encoding')
) {

// Check if mbstring extension is loaded
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->message_limit) {
$value = substr($value, 0, $this->message_limit - 10) . ' {clipped}';
if (mb_strlen($value) > $this->message_limit) {
$value = mb_substr($value, 0, $this->message_limit - 10, 'UTF-8') . ' {clipped}';
}
} else {
if (strlen($value) > $this->message_limit) {
$value = substr($value, 0, $this->message_limit - 10) . ' {clipped}';
}
}

return $value;
Expand Down
25 changes: 21 additions & 4 deletions test/Raven/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public function testLongString()
for ($i = 0; $i < 100; $i++) {
foreach (array(100, 1000, 1010, 1024, 1050, 1100, 10000) as $length) {
$input = '';
for ($i = 0; $i < $length; $i++) {
$input .= chr(mt_rand(0, 255));
for ($j = 0; $j < $length; $j++) {
$input .= chr(mt_rand(ord('a'), ord('z')));
}
$result = $serializer->serialize($input);
$this->assertInternalType('string', $result);
Expand All @@ -141,8 +141,8 @@ public function testLongStringWithOverwrittenMessageLength()
for ($i = 0; $i < 100; $i++) {
foreach (array(100, 490, 499, 500, 501, 1000, 10000) as $length) {
$input = '';
for ($i = 0; $i < $length; $i++) {
$input .= chr(mt_rand(0, 255));
for ($j = 0; $j < $length; $j++) {
$input .= chr(mt_rand(ord('a'), ord('z')));
}
$result = $serializer->serialize($input);
$this->assertInternalType('string', $result);
Expand All @@ -164,4 +164,21 @@ public function testSerializeValueResource()
$this->assertInternalType('string', $result);
$this->assertEquals('Resource stream', $result);
}

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

$teststring = 'Прекратите надеяться, что ваши пользователи будут сообщать об ошибках';
$serializer = new Raven_Serializer(null, 19); // Length of 19 will clip character in half if no mb_* string functions are used for the teststring

$clipped = $serializer->serialize($teststring);
$this->assertEquals('Прекратит {clipped}', $clipped);

Raven_Compat::json_encode($clipped);

$this->assertEquals(JSON_ERROR_NONE, json_last_error());
}
}