BSTR.OPS.EQS

Incorrect equality comparison of BSTR values

The BSTR.OPS.COMP checker finds instances of equality comparisons with BSTR entities. Equality comparison operations can't be used on BSTR values, as they are meaningless in the BSTR context and can cause incorrect memory usage if they're not corrected.

Vulnerability and risk

Because 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 prevention

Unlike 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.

Vulnerable code example

Copy
  void bstr_eqs() {
       BSTR foo;
       wchar_t *bar = L"abc";
       if (foo != bar) 
               bar = NULL; 
  }

Klocwork flags line 4 in which an equality comparison operation is attempted on a BSTR value.

Related checkers