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