File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
src/Likely Bugs/Memory Management
test/query-tests/Likely Bugs/Memory Management/ReturnStackAllocatedMemory Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 13
13
14
14
import cpp
15
15
import semmle.code.cpp.dataflow.EscapesTree
16
+ import semmle.code.cpp.models.interfaces.PointerWrapper
16
17
import semmle.code.cpp.dataflow.DataFlow
17
18
18
19
/**
@@ -39,6 +40,10 @@ predicate hasNontrivialConversion(Expr e) {
39
40
e instanceof ParenthesisExpr
40
41
)
41
42
or
43
+ // A smart pointer can be stack-allocated while the data it points to is heap-allocated.
44
+ // So we exclude such "conversions" from this predicate.
45
+ e = any ( PointerWrapper wrapper ) .getAnUnwrapperFunction ( ) .getACallToThisFunction ( )
46
+ or
42
47
hasNontrivialConversion ( e .getConversion ( ) )
43
48
}
44
49
Original file line number Diff line number Diff line change @@ -189,3 +189,30 @@ int *&conversionInFlow() {
189
189
int *&pRef = p; // has conversion in the middle of data flow
190
190
return pRef; // BAD [NOT DETECTED]
191
191
}
192
+
193
+ namespace std {
194
+ template <typename T>
195
+ class shared_ptr {
196
+ public:
197
+ shared_ptr () noexcept ;
198
+ explicit shared_ptr (T*);
199
+ shared_ptr (const shared_ptr&) noexcept ;
200
+ template <class U > shared_ptr (const shared_ptr<U>&) noexcept ;
201
+ template <class U > shared_ptr (shared_ptr<U>&&) noexcept ;
202
+
203
+ shared_ptr<T>& operator =(const shared_ptr<T>&) noexcept ;
204
+ shared_ptr<T>& operator =(shared_ptr<T>&&) noexcept ;
205
+
206
+ T& operator *() const noexcept ;
207
+ T* operator ->() const noexcept ;
208
+
209
+ T* get () const noexcept ;
210
+ };
211
+ }
212
+
213
+ auto make_read_port ()
214
+ {
215
+ auto port = std::shared_ptr<int >(new int );
216
+ auto ptr = port.get ();
217
+ return ptr; // GOOD
218
+ }
You can’t perform that action at this time.
0 commit comments