4

I have following C++ code:

#include <memory>
#include <vector>
#include <string>
#include <unordered_map>

void erase_from_vector(std::vector<std::weak_ptr<int>> &mvec) {
    for (auto mvec_it = mvec.begin(); mvec_it != mvec.end(); )
        mvec_it = mvec.erase(mvec_it);
}

int main(void) {
#if 0
    std::vector<std::weak_ptr<int>> mvec;
    for (auto mvec_it = mvec.begin(); mvec_it != mvec.end(); )
        mvec_it = mvec.erase(mvec_it);
#endif
}

GCC generates warning when I compile it this way:

ppk@fif-cloud-dev:~$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

ppk@fif-cloud-dev:~$ g++ -fstrict-overflow -Wstrict-overflow=5 -O2 -std=c++14  warn1.cc
warn1.cc: In function ‘void erase_from_vector(std::vector<std::weak_ptr<int> >&)’:
warn1.cc:6:6: warning: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Wstrict-overflow]
 void erase_from_vector(std::vector<std::weak_ptr<int>> &mvec) {
      ^

But when I change -O2 flag to -O1 it compiles without any warnings. When I keep flag -O2 and uncomment code in main() it also compiles without any warnings. Clang compiler also does not report any warnings.

I suppose that this warning comes from std::weak_ptr destructor where counter is decremented but have no idea why it appears in my code.

Is the warning caused by my error or error in the compiler?

2
  • 2
    Why do so many people misunderstand warning as error? The warning is not telling you there is any error, it is just telling you that it is assuming that the code is correct, which can hardly be wrong. Ok, that's a rather useless warning, and it is likely to get removed in a future version, with most (all?) of Wstrict-overflow... Commented May 24, 2017 at 11:57
  • Because cause some of warnings can be serious and hard to debug runtime error. Sometimes it is better to spend more time analyzing warnings than more time in the future correcting such errors. Especially when you create software with critical reliability/security requirements. Commented May 24, 2017 at 13:09

1 Answer 1

3

Most likely a quirk of gcc 5.4. It's gone as soon as you get to gcc 6.1, and I don't see it reappear again in any later version.

gcc 5.4 (warnings)

gcc 6.1 (no warnings)

It's especially damning that Clang doesn't reproduce the behavior.

It should be noted that such behavior isn't exactly a bug, according to the doc (emphasis mine)

An optimization that assumes that signed overflow does not occur is perfectly safe if the values of the variables involved are such that overflow never does, in fact, occur. Therefore this warning can easily give a false positive: a warning about code that is not actually a problem.

That you're using -Wstrict-overflow=5 makes it even more likely, as this is the highest warning level that comes with its own disclaimer:

this warning level gives a very large number of false positives

My suggestion is to either upgrade your compiler or accept that gcc 5.4 is going to give you a false positive here.

Sign up to request clarification or add additional context in comments.

1 Comment

Or don't enable warnings that are documented to produce false positives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.