Recently updated Cppcheck to newest version (2.1) and this error appeared in many locations in our code. I prepared the simplest code that shows the error:
I know that the for loop is useless here but it is here only to show when the error occurs. The get function returns valid pointer to first element of someContainer.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't think cppcheck could ever analyse all the code from the OP and determine that only addresses inside the container are returned. On the other hand, if cppcheck didn't warn in this case just to be safe, then not many instances of returnDanglingLifetime would remain.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
On the other hand, if cppcheck didn't warn in this case just to be safe, then not many instances of returnDanglingLifetime would remain.
That is true however our philosophy is to avoid false positives when we are unsure. I am hoping there will still be many instances that we can warn about.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't think the text of the warning is too far off. I guess there just is no differentiation between simple variables and containers.
The heuristic for avoiding the warning entirely would be something like this:
If there is a container-like object O locally created from another container C, and a function seemingly returns addresses to elements in O, assume that there is operator overloading and other shenanigans going on, so that the returned address safely points at an element in C.
I think there's a trade-of between "losing useful warnings" and "accomodating crazy code"...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Recently updated Cppcheck to newest version (2.1) and this error appeared in many locations in our code. I prepared the simplest code that shows the error:
I know that the for loop is useless here but it is here only to show when the error occurs. The get function returns valid pointer to first element of someContainer.
I can reproduce this with v2.3.
This snippet still shows the problem:
I don't think cppcheck could ever analyse all the code from the OP and determine that only addresses inside the container are returned. On the other hand, if cppcheck didn't warn in this case just to be safe, then not many instances of returnDanglingLifetime would remain.
hmm.. cppcheck shouldn't warn unless it can determine that there is a bug.
The error message is not 100% correct:
Returning object that points to local variable 'seq' that will be invalid when returning.
The
r
does not point atseq
but it points at a member inseq
.I have created ticket https://trac.cppcheck.net/ticket/10163
That is true however our philosophy is to avoid false positives when we are unsure. I am hoping there will still be many instances that we can warn about.
I don't think the text of the warning is too far off. I guess there just is no differentiation between simple variables and containers.
The heuristic for avoiding the warning entirely would be something like this:
If there is a container-like object O locally created from another container C, and a function seemingly returns addresses to elements in O, assume that there is operator overloading and other shenanigans going on, so that the returned address safely points at an element in C.
I think there's a trade-of between "losing useful warnings" and "accomodating crazy code"...