BSTR.IA.ASSIGNBSTR variable is assigned a non-BSTR valueThe BSTR.IA.ASSIGN checker finds code in which BSTR variable is assigned a non-BSTR value. BSTR variables can be assigned only null constants or values of type BSTR. Vulnerability and riskBecause the two styles are constructed differently, converting COM-style BSTR strings to and from C-style strings needs care. In some cases, conversions between the two compile well, but still produce unexpected results. Mitigation and preventionUnlike C-style strings, BSTR strings have a 4-byte length prefix that contains the number of bytes in the following data string. BSTR strings can also contain embedded null characters, and aren't strongly typed. For these reasons, it's best not to use BSTR in new designs. For existing interfaces, it's important to make conversions and use the Sys*Alloc*, SysFree* and Sys*String* memory allocation functions carefully. Vulnerable code example1 void bstr_assign() { 2 BSTR foo; 3 foo = L"abc"; 4 } Klocwork flags line 3, in which the BSTR variable is assigned a non-BSTR value. Fixed code example1 void bstr_assign() { 2 BSTR foo = SysAllocString(L"abc"); 3 } In this fixed example, we assume that the variable bar was intended to be a BSTR, so it's defined as BSTR using the correct SysAllocString conversion function. Related checkers |