|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63208 BSTR to PHP string conversion not binary safe
Submitted: 2012-10-03 12:40 UTC Modified: 2020-06-25 08:38 UTC
Avg. Score:4.4 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: gopeyx at gmail dot com Assigned: cmb (profile)
Status: Closed Package: COM related
PHP Version: 5.3.17 OS: Windows NT W-AM-13079 6.1 build
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: gopeyx at gmail dot com
New email:
PHP Version: OS:


 [2012-10-03 12:40 UTC] gopeyx at gmail dot com
I have a VARIANT of type 8209 which is an array of bytes (VT_ARRAY | VT_UI1).
This array contains null byte in the middle.

When trying to convert it to a string only bytes before null byte are used. Others are ignored.

But they are present in the object. You can see it if you walk through the bytes of variant via foreach().

Test script:

$string = "ab\0cd";

$variant = new VARIANT($string, VT_ARRAY | VT_UI1); // Array of bytes

$converted = (string) $variant;


Expected result:
string(5) "abcd"
string(5) "abcd"

// $converted variable should be equal to the original $string and should contain 5 characters.

Actual result:
string(5) "abcd"
string(2) "ab"


olestring-binary-safe (last revision 2015-08-16 16:26 UTC by

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-11 15:38 UTC] tomas dot var at dtr-sistemas dot com
I have the same problem when I want to extract the value of a LONGBINARY field from an MS-Access or MSSQL Database (OLE Field exactly), and the OLE embeeded header contains some zero bytes. I can obtain only 3 bytes when it converts to a string for save to a file. And when I loop with "foreach" the VARIANT ARRAY of UNSIGNED BYTES it loops correctly (except for the byte '\0' after every valid byte) and if I count the array with "count($field->Value)" for example, it results the double lenght that expects.
Why don't exists in PHP a way to obtain a simple array of bytes for a variable, or access the data like a "string" of bytes?
 [2015-08-16 16:25 UTC]
-Status: Open +Status: Analyzed
 [2015-08-16 16:25 UTC]
The problem is that php_com_olestring_to_string()[1] is not binary
safe, because it passes -1 to the cchWideChar parameter of
WideCharToMultiByte()[2]. That causes WideCharToMultiByte() to
stop at the first null character (actually, two null bytes), and
so the rest of the string is ignored.

As a BSTR[3] has a prefix containing its length in bytes it
appears we could use this to calculate the proper cchWideChar
argument, see the attached patch.

[1] <>
[2] <>
[3] <>
 [2015-08-16 16:26 UTC]
The following patch has been added/updated:

Patch Name: olestring-binary-safe
Revision:   1439742362
 [2020-06-25 08:38 UTC]
-Summary: Wrong conversion of byte array VARIANT which contains null byte to string +Summary: BSTR to PHP string conversion not binary safe -Assigned To: +Assigned To: cmb
 [2020-06-25 08:41 UTC]
The following pull request has been associated:

Patch Name: Fix #63208: BSTR to PHP string conversion not binary safe
On GitHub:
 [2020-06-29 17:07 UTC]
Automatic comment on behalf of
Log: Fix #63208: BSTR to PHP string conversion not binary safe
 [2020-06-29 17:07 UTC]
-Status: Analyzed +Status: Closed
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Dec 01 18:05:54 2022 UTC