rtl::OUString::compareToAscii is dangerous

Stephan Bergmann sbergman at redhat.com
Tue Mar 19 09:54:34 PDT 2013


There are two overloads of rtl::OUString::compareToAscii:

* compareToAscii(char const * asciiStr) does a full comparison of *this 
and the given asciiStr.

* compareToAscii(char cosnt * asciiStr, sal_Int32 maxLength) does a 
shortened comparison of only up to maxLength characters of *this and the 
given asciiStr.

That is,

   s.compareToAscii(RTL_CONSTASCII_STRINGPARAM("foo")) == 0

translates to

   s.startsWith("foo")

rather than any of

   s.compareTo("foo") == 0

   s == "foo"

This subtlety has introduced various bugs with recent string clean-up 
commits, so I tried to systematically scrutinize the output of "git log 
-ScompareToAscii --patch" now, at least the range of master commits from 
recent 7cd539319943f43b6f66b72ad85ecc0012d3e007 back through the start 
of 2011.

Please be very careful when you create or review commits that involve 
clean-up of compareToAscii.

Stephan


More information about the LibreOffice mailing list