Menu

portability check - looping on eof only

2015-11-12
2015-11-18
  • AnotherUser

    AnotherUser - 2015-11-12

    Dear cppcheck developers,

    I recently encountered the following bug, looping on eof() is not stable unless you check the good() bit. This works under some platforms but not others (linux OK, windows (mingw) - not OK). This could be a candidate for a check.

    The check could test to see if the ifstream object is accessed in any other way (ie checking good() bit).

    If it is not, then the "text.txt" file becomes a potential DOS vector if it is not trusted. If the test is to be more conservative, it could check for any "break" statements which might otherwise allow loop exit by another means.

    #include <fstream>
    
    using namespace std;
    
    int main()
    {
            ifstream file("text.txt");
            string str;
            while(!file.eof())
                    getline(file,str);
    }
    

    Thanks in advance.

     
  • Daniel Marjamäki

    Thanks! This is interesting. I imagine there are many such bugs. Can you please report this in our issue tracker so we don't forget it? http://trac.cppcheck.net

     
  • AnotherUser

    AnotherUser - 2015-11-18

    I tried to get a trac login, but did not receive a confirmation email. Can someone with access please add this?

     

Log in to post a comment.