#40664 [NEW]: String conversion functions wrong for multibyte chars

From: Date: Wed, 28 Feb 2007 11:38:27 +0000
Subject: #40664 [NEW]: String conversion functions wrong for multibyte chars
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-109698@lists.php.net to get a copy of this message
From:             fjortiz at comunet dot es
Operating system: Win32
PHP version:      5.2.1
PHP Bug Type:     COM related
Bug description:  String conversion functions wrong for multibyte chars

Description:
------------
when converting a UTF-8 encoded, multibyte string (Russian for example),
to a COM string, a wrong number of bytes are allocated, thus creating the
COM string with junk bytes at the end.

For example, when I pass my COM-based ADODB driver a 5-letter word in
Russian, I get at destination a 10 (5*2) characters string, the first 5
are the right Russian chars and the rest 5 are junk characters.

This was also explained for 4.4.2 in bug #37899


Actual result:
--------------
this is solved patching two files:
\ext\com_dotnet\com_variant.c, function php_com_variant_from_zval, line
156:

156,157c156
< 			V_BSTR(v) = SysAllocString((char*)olestring);
---
> 			V_BSTR(v) = SysAllocStringByteLen((char*)olestring, Z_STRLEN_P(z) *
sizeof(OLECHAR));

\ext\com_dotnet\com_olechar.c, function php_com_string_to_olestring:

37d36
< 	uint unicode_strlen;
39,40c38,44
< 	unicode_strlen = MultiByteToWideChar(codepage, (codepage == CP_UTF8 ?
< 		0 : MB_PRECOMPOSED | MB_ERR_INVALID_CHARS), string, -1, NULL, 0);
---
> 	if (string_len == -1) {
> 		/* determine required length for the buffer (includes NUL terminator)
*/
> 		string_len = MultiByteToWideChar(codepage, flags, string, -1, NULL,
0);
> 	} else {
> 		/* allow room for NUL terminator */
> 		string_len++;
> 	}
42,44c46,48
< 	if (unicode_strlen > 0) {
< 		olestring = (OLECHAR*)safe_emalloc(sizeof(OLECHAR), unicode_strlen,
0);
< 		ok = MultiByteToWideChar(codepage, flags, string, -1, olestring,
unicode_srlen);
---
> 	if (strlen > 0) {
> 		olestring = (OLECHAR*)safe_emalloc(sizeof(OLECHAR), string_len, 0);
> 		ok = MultiByteToWideChar(codepage, flags, string, string_len,
olestring, string_len);



-- 
Edit bug report at http://bugs.php.net/?id=40664&edit=1
-- 
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40664&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40664&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40664&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=40664&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=40664&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=40664&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=40664&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=40664&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=40664&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=40664&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=40664&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=40664&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=40664&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=40664&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=40664&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=40664&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=40664&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=40664&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=40664&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=40664&r=mysqlcfg


Thread (15 messages)

« previous php.bugs (#109698) next »