php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61362 Exception::getTraceAsString, Exception::__toString not able to handle unicode
Submitted: 2012-03-12 15:44 UTC Modified: 2015-06-17 15:01 UTC
Votes:8
Avg. Score:3.9 ± 1.2
Reproduced:6 of 6 (100.0%)
Same Version:3 (50.0%)
Same OS:4 (66.7%)
From: sam at rmcreative dot ru Assigned:
Status: Closed Package: Unicode Engine related
PHP Version: 7.0.0 OS: All
Private report: No CVE-ID: None
 [2012-03-12 15:44 UTC] sam at rmcreative dot ru
Description:
------------
Exception::getTraceAsString, Exception::__toString are not able to properly handle unicode in stack trace. Instead of showing actual argument values it's giving ???? in case value is unicode.

Tested on PHP 5.3 and PHP 5.4.

Test script:
---------------
<?php
function test($arg){
	throw new Exception();
}

try {
	test('тест');
}
catch(Exception $e) {
	echo $e->getTraceAsString();
	echo (string)$e;
}

Expected result:
----------------
d:\web\usr\local\php54>php.exe d:\src\exception_wrong_trace\test.php
#0 D:\src\exception_wrong_trace\test.php(7): test('тест')
#1 {main}exception 'Exception' in D:\src\exception_wrong_trace\test.php:3
Stack trace:
#0 D:\src\exception_wrong_trace\test.php(7): test('тест')
#1 {main}

Actual result:
--------------
d:\web\usr\local\php54>php.exe d:\src\exception_wrong_trace\test.php
#0 D:\src\exception_wrong_trace\test.php(7): test('????')
#1 {main}exception 'Exception' in D:\src\exception_wrong_trace\test.php:3
Stack trace:
#0 D:\src\exception_wrong_trace\test.php(7): test('????')
#1 {main}

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-14 12:20 UTC] tony2001@php.net
-Status: Open +Status: Feedback
 [2012-03-14 12:20 UTC] tony2001@php.net
What if you try to switch your browser encoding to UTF8 from whatever it is by default?
 [2012-03-14 15:14 UTC] tony2001@php.net
Nevermind, I see the problem.
 [2012-03-14 15:14 UTC] tony2001@php.net
-Status: Feedback +Status: Open
 [2012-03-14 15:16 UTC] sam at rmcreative dot ru
It's UTF8 already.
 [2012-03-14 17:20 UTC] tony2001@php.net
The problem is here: http://lxr.php.net/opengrok/xref/PHP_5_4/Zend/zend_exceptions.c#391
Dunno what to do in this case: either add to the docs that the result of getTraceAsString() is perfectly safe from binary symbols or drop that code completely and allow any chars there.
 [2012-03-16 15:03 UTC] tony2001@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: johannes
 [2015-06-17 14:17 UTC] cmb@php.net
-PHP Version: 5.4.0 +PHP Version: 7.0.0
 [2015-06-17 14:17 UTC] cmb@php.net
See <http://3v4l.org/ONJBe>. As of PHP 5.4.29 the result is
correct, but master shows a regression.

The problem is that c is declared as char[1], what is a signed
value, but the following calculations assume it is unsigned.
Simply declaring

        unsigned char c = s[i];

should solve the problem.

I'm working on a PR (including a respective test).

[1] <http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_exceptions.c#402>
 [2015-06-17 14:43 UTC] johannes@php.net
-Status: Assigned +Status: Open -Assigned To: johannes +Assigned To:
 [2015-06-17 15:01 UTC] cmb@php.net
-Status: Open +Status: Analyzed
 [2015-06-17 20:31 UTC] nikic@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=653c869348b7626d43a30f67be9362fbcce10d2b
Log: Fix #61362: Exception::getTraceAsString and ::__toString scramble Unicode
 [2015-06-17 20:31 UTC] nikic@php.net
-Status: Analyzed +Status: Closed
 [2015-06-23 18:04 UTC] ab@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=653c869348b7626d43a30f67be9362fbcce10d2b
Log: Fix #61362: Exception::getTraceAsString and ::__toString scramble Unicode
 [2016-07-20 11:38 UTC] davey@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=653c869348b7626d43a30f67be9362fbcce10d2b
Log: Fix #61362: Exception::getTraceAsString and ::__toString scramble Unicode
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC