richelbilderbeek / globulation2

Get Globulations 2's code to work
GNU General Public License v3.0
0 stars 0 forks source link

cppcheck error: iterator 'bi' used after element has been erased #4

Open richelbilderbeek opened 5 years ago

richelbilderbeek commented 5 years ago

When running cppcheck (see Travis build log), it detects the following errors:

[Building.cpp:2783]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Building.cpp:2788]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Building.cpp:2821]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Building.cpp:2830]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Building.cpp:2840]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Building.cpp:2860]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Building.cpp:2878]: (error) Shifting signed 32-bit value by 31 bits is undefined behaviour
[Game.cpp:1560] -> [Game.cpp:1559]: (error) Iterator 'bi' used after element has been erased.

Focusing on the last error:

[Game.cpp:1560] -> [Game.cpp:1559]: (error) Iterator 'bi' used after element has been erased.

These are the lines it is about:

for (std::list<Building *>::iterator bi=teams[ti]->virtualBuildings.begin(); bi!=teams[ti]->virtualBuildings.end(); ++bi)
    if ((*bi)->posX==x && (*bi)->posY==y)
    {
        teams[ti]->virtualBuildings.erase(bi);                      //1559
        teams[ti]->myBuildings[Building::GIDtoID((*bi)->gid)]=NULL; //1560
        delete *bi;
        found=true;
        break;
    }

Changing the order to this fixes the cppcheck error:

for (std::list<Building *>::iterator bi=teams[ti]->virtualBuildings.begin(); bi!=teams[ti]->virtualBuildings.end(); ++bi)
    if ((*bi)->posX==x && (*bi)->posY==y)
    {
        teams[ti]->myBuildings[Building::GIDtoID((*bi)->gid)]=NULL;
        delete *bi;
        teams[ti]->virtualBuildings.erase(bi);
        found=true;
        break;
    }
richelbilderbeek commented 5 years ago

Submitted this bug as a Bitbucket Issue