Skip to content

Commit 7ac5e84

Browse files
committed
C++: Add make_shared and make_unique test cases
1 parent 075ce6e commit 7ac5e84

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,26 @@
439439
| movableclass.cpp:65:13:65:18 | call to source | movableclass.cpp:65:13:65:20 | call to MyMovableClass | TAINT |
440440
| movableclass.cpp:65:13:65:20 | call to MyMovableClass | movableclass.cpp:65:8:65:9 | ref arg s3 | TAINT |
441441
| movableclass.cpp:65:13:65:20 | call to MyMovableClass | movableclass.cpp:65:11:65:11 | call to operator= | TAINT |
442+
| smart_pointer.cpp:11:30:11:50 | call to make_shared | smart_pointer.cpp:12:11:12:11 | p | |
443+
| smart_pointer.cpp:11:30:11:50 | call to make_shared | smart_pointer.cpp:13:10:13:10 | p | |
444+
| smart_pointer.cpp:17:32:17:54 | call to make_shared | smart_pointer.cpp:18:11:18:11 | p | |
445+
| smart_pointer.cpp:17:32:17:54 | call to make_shared | smart_pointer.cpp:19:10:19:10 | p | |
446+
| smart_pointer.cpp:23:30:23:50 | call to make_unique | smart_pointer.cpp:24:11:24:11 | p | |
447+
| smart_pointer.cpp:23:30:23:50 | call to make_unique | smart_pointer.cpp:25:10:25:10 | p | |
448+
| smart_pointer.cpp:29:32:29:54 | call to make_unique | smart_pointer.cpp:30:11:30:11 | p | |
449+
| smart_pointer.cpp:29:32:29:54 | call to make_unique | smart_pointer.cpp:31:10:31:10 | p | |
450+
| smart_pointer.cpp:35:30:35:50 | call to make_shared | smart_pointer.cpp:37:6:37:6 | p | |
451+
| smart_pointer.cpp:35:30:35:50 | call to make_shared | smart_pointer.cpp:38:10:38:10 | p | |
452+
| smart_pointer.cpp:35:30:35:50 | call to make_shared | smart_pointer.cpp:39:11:39:11 | p | |
453+
| smart_pointer.cpp:37:5:37:17 | ... = ... | smart_pointer.cpp:37:5:37:5 | call to operator* [post update] | |
454+
| smart_pointer.cpp:37:10:37:15 | call to source | smart_pointer.cpp:37:5:37:17 | ... = ... | |
455+
| smart_pointer.cpp:38:10:38:10 | ref arg p | smart_pointer.cpp:39:11:39:11 | p | |
456+
| smart_pointer.cpp:43:29:43:51 | call to unique_ptr | smart_pointer.cpp:45:6:45:6 | p | |
457+
| smart_pointer.cpp:43:29:43:51 | call to unique_ptr | smart_pointer.cpp:46:10:46:10 | p | |
458+
| smart_pointer.cpp:43:29:43:51 | call to unique_ptr | smart_pointer.cpp:47:11:47:11 | p | |
459+
| smart_pointer.cpp:45:5:45:17 | ... = ... | smart_pointer.cpp:45:5:45:5 | call to operator* [post update] | |
460+
| smart_pointer.cpp:45:10:45:15 | call to source | smart_pointer.cpp:45:5:45:17 | ... = ... | |
461+
| smart_pointer.cpp:46:10:46:10 | ref arg p | smart_pointer.cpp:47:11:47:11 | p | |
442462
| standalone_iterators.cpp:39:45:39:51 | source1 | standalone_iterators.cpp:40:11:40:17 | source1 | |
443463
| standalone_iterators.cpp:39:45:39:51 | source1 | standalone_iterators.cpp:41:12:41:18 | source1 | |
444464
| standalone_iterators.cpp:39:45:39:51 | source1 | standalone_iterators.cpp:42:14:42:20 | source1 | |
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "stl.h"
2+
3+
int source();
4+
void sink(int);
5+
void sink(int*);
6+
7+
template<typename T> void sink(std::shared_ptr<T>&);
8+
template<typename T> void sink(std::unique_ptr<T>&);
9+
10+
void test_make_shared() {
11+
std::shared_ptr<int> p = std::make_shared<int>(source());
12+
sink(*p); // tainted [NOT DETECTED]
13+
sink(p); // tainted [NOT DETECTED]
14+
}
15+
16+
void test_make_shared_array() {
17+
std::shared_ptr<int[]> p = std::make_shared<int[]>(source());
18+
sink(*p); // not tainted
19+
sink(p); // not tainted
20+
}
21+
22+
void test_make_unique() {
23+
std::unique_ptr<int> p = std::make_unique<int>(source());
24+
sink(*p); // tainted [NOT DETECTED]
25+
sink(p); // tainted [NOT DETECTED]
26+
}
27+
28+
void test_make_unique_array() {
29+
std::unique_ptr<int[]> p = std::make_unique<int[]>(source());
30+
sink(*p); // not tainted
31+
sink(p); // not tainted
32+
}
33+
34+
void test_reverse_taint_shared() {
35+
std::shared_ptr<int> p = std::make_shared<int>();
36+
37+
*p = source();
38+
sink(p); // tainted [NOT DETECTED]
39+
sink(*p); // tainted [NOT DETECTED]
40+
}
41+
42+
void test_reverse_taint_unique() {
43+
std::unique_ptr<int> p = std::unique_ptr<int>();
44+
45+
*p = source();
46+
sink(p); // tainted [NOT DETECTED]
47+
sink(*p); // tainted [NOT DETECTED]
48+
}

cpp/ql/test/library-tests/dataflow/taint-tests/stl.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,39 @@ namespace std {
224224
void clear() noexcept;
225225
};
226226
}
227+
228+
// --- make_shared / make_unique ---
229+
230+
namespace std {
231+
template<typename T>
232+
class shared_ptr {
233+
public:
234+
shared_ptr() noexcept;
235+
explicit shared_ptr(T*);
236+
template<class U> shared_ptr(const shared_ptr<U>&) noexcept;
237+
template<class U> shared_ptr(shared_ptr<U>&&) noexcept;
238+
239+
shared_ptr<T>& operator=(const shared_ptr<T>&) noexcept;
240+
shared_ptr<T>& operator=(shared_ptr<T>&&) noexcept;
241+
242+
T& operator*() const noexcept;
243+
T* operator->() const noexcept;
244+
};
245+
246+
template<typename T>
247+
class unique_ptr {
248+
public:
249+
constexpr unique_ptr() noexcept;
250+
explicit unique_ptr(T*) noexcept;
251+
unique_ptr(unique_ptr<T>&&) noexcept;
252+
253+
unique_ptr<T>& operator=(unique_ptr<T>&&) noexcept;
254+
255+
T& operator*() const;
256+
T* operator->() const noexcept;
257+
};
258+
259+
template<typename T, class... Args> unique_ptr<T> make_unique(Args&&...);
260+
261+
template<typename T, class... Args> shared_ptr<T> make_shared(Args&&...);
262+
}

0 commit comments

Comments
 (0)