Search code examples
c++gccboosttdm-mingw

C++ GCC4.4 warning: array subscript is above array bounds


I recently upgraded to GCC 4.4 (MinGW TDM build) and now the follow code produces these warning:

In member function 'void Console::print(const std::string&)':

warning: array subscript is above array bounds

Here's the code:

void Console::print( const std::string& str ) {
        std::string newLine( str );
        if( newLine.size() > MAX_LINE_LENGTH ) {
            sf::Uint32 stringSize = newLine.size();
            for( sf::Uint32 insertPos = MAX_LINE_LENGTH;
                    insertPos < stringSize; insertPos += MAX_LINE_LENGTH ) {
                newLine.insert( insertPos, "\n" );
            }
        }

        StringList tokens;
        boost::split( tokens, newLine, boost::is_any_of("\n") );

        for( StringList::iterator it = tokens.begin();
                it != tokens.end(); ++it ) {
            addLine( *it );
        }
    }

Any ideas?


It is the optimizations that are doing it...

Also it appears to be this line which is causing it:

boost::split( tokens, newLine, boost::is_any_of("\n") );

Ah yes, I found it, it is the argument for boost::is_any_of(), by wrapping it in a string() constructor the warning goes away, thank you all for your help :)

boost::split( tokens, newLine, boost::is_any_of( string( "\n" ) ) );

Solution

  • Got the same error. As a workaround I replaced

    is_any_of(" ")
    

    with

    is_from_range(' ', ' ')
    

    which might also be slightly more efficient.