BSTR.OPS.ARITHM

Incorrect arithmetic operation with BSTR value

The BSTR.OPS.ARITHM checker finds instances of address arithmetics with BSTR entities. Using these operations on BSTR values leads to incorrect memory usage.

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_arithm() {
       BSTR foo = SysAllocString(L"abc");
       foo++; 
  }

Klocwork flags line 3 in which an arithmetic operation is attempted on a BSTR value.

Related checkers