This code:
#include <set> struct Somestruct { int a; int b; Somestruct(int a, int b) : a(a), b(b) {} } typedef set<Somestruct> SomeSet; static SomeSet stuff; Somestruct *addit() { std::pair<SomeSet::iterator,bool> result(stuff.insert(Somestruct(1, 2))); Somestruct * impl = const_cast<Somestruct*>(&*result.first); return impl; }
generates this output
$ cppcheck test.cc Checking test.cc ... test.cc:13:12: error: Returning pointer that will be invalid when returning. [returnDanglingLifetime] return impl; ^ test.cc:12:49: note: Address of variable taken here. Somestruct * impl = const_cast<Somestruct*>(&*result.first); ^ test.cc:13:12: note: Returning pointer that will be invalid when returning. return impl; ^
as far as I can tell that is entirely valid because it is just de-referencing the iterator which points to something inside the set.
(cppcheck 2.8)
Thanks! I have created https://trac.cppcheck.net/ticket/11168
looks a bit tricky to fix. but of course it should be fixed.
thanks. appreciated
Log in to post a comment.
This code:
generates this output
as far as I can tell that is entirely valid because it is just de-referencing the iterator which points to something inside the set.
(cppcheck 2.8)
Last edit: T Tanner 2022-07-02
Thanks! I have created https://trac.cppcheck.net/ticket/11168
looks a bit tricky to fix. but of course it should be fixed.
thanks. appreciated