std::vector<> aFoo(3) type clang plugin request ? =)

Michael Meeks michael.meeks at collabora.com
Sat Jun 20 02:23:40 PDT 2015


Hi Noel,

	So - another annoying clang plugin request ;-) I just fell over:

	https://bugs.documentfoundation.org/show_bug.cgi?id=92194

	where by 'fell' I mean several hours of debugging horrible template
mess [ which is inevitably routinely going down the slow paths and we'd
do way better to use pixman IMNSHO ;-]. Anyhow - at the bottom was this:

             std::vector< basebmp::Color > aDevPal(2);
-            aDevPal.push_back( basebmp::Color( 0, 0, 0 ) );
-            aDevPal.push_back( basebmp::Color( 0xff, 0xff, 0xff ) );
+            aDevPal[0] = basebmp::Color( 0, 0, 0 );
+            aDevPal[1] = basebmp::Color( 0xff, 0xff, 0xff );

	Basically a mis-understanding of how std::vector works when you
pre-allocate its size - these slots get some (default) contents - in
this case the masks' 1 & 0 state were both default black ;-)

	Anyhow - I imagine there are a ton of these around the code-base; Tor
did some quick greps and quickly found:

mysqlc/source/mysqlc_databasemetadata.cxx:1080

    for (sal_uInt32 i = 0; i < 2; i++) {
        if (m_rConnection.getMysqlVersion() >= requiredVersion[i]) {
            std::vector< Any > aRow(1);
             aRow.push_back(makeAny(mysqlc_sdbc_driver::convert(table_types[i], encoding)));

	"and many other similar places in mysqlc"

sax/qa/cppunit/test_converter.cxx:667

    std::vector< sal_Int8 > tempSeq(4);
    for(sal_Int8 i = 0; i<4; ++i)
        tempSeq.push_back(i);


	Which also looks highly suspicious =)

	And I was wondering ... could there be a clang plugin for that ? in
general I think it's prolly a good swap to discard the use-case of
someone wanting <N> default-initialized items at the front of a vector
and always warn on it ;-)

	So I suppose the check would be for a size reservation on a vector
followed by push_back - rather than some array indexing - does that make
sense ? or did I go crazy ;-)

	Thanks,

		Michael.

-- 
 michael.meeks at collabora.com  <><, Pseudo Engineer, itinerant idiot



More information about the LibreOffice mailing list