Skip to content

Commit e10caa6

Browse files
committed
C++: Add tests.
1 parent e94cde9 commit e10caa6

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/UseOfStringAfterLifetimeEnds.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| test.cpp:165:34:165:38 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
2+
| test.cpp:166:39:166:43 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
3+
| test.cpp:167:44:167:48 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
4+
| test.cpp:169:29:169:33 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
5+
| test.cpp:178:37:178:41 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
6+
| test.cpp:181:39:181:43 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
7+
| test.cpp:183:37:183:41 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
8+
| test.cpp:187:31:187:35 | call to c_str | The underlying string object is destroyed after the call to 'c_str' returns. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Security/CWE/CWE-416/UseOfStringAfterLifetimeEnds.ql
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
typedef unsigned long size_t;
2+
3+
namespace std {
4+
template<class T> struct remove_reference { typedef T type; };
5+
6+
template<class T> struct remove_reference<T &> { typedef T type; };
7+
8+
template<class T> struct remove_reference<T &&> { typedef T type; };
9+
10+
template<class T> using remove_reference_t = typename remove_reference<T>::type;
11+
12+
template< class T > std::remove_reference_t<T>&& move( T&& t );
13+
}
14+
15+
// --- iterator ---
16+
17+
namespace std {
18+
template<class T> struct remove_const { typedef T type; };
19+
20+
template<class T> struct remove_const<const T> { typedef T type; };
21+
22+
// `remove_const_t<T>` removes any `const` specifier from `T`
23+
template<class T> using remove_const_t = typename remove_const<T>::type;
24+
25+
struct ptrdiff_t;
26+
27+
template<class I> struct iterator_traits;
28+
29+
template <class Category,
30+
class value_type,
31+
class difference_type = ptrdiff_t,
32+
class pointer_type = value_type*,
33+
class reference_type = value_type&>
34+
struct iterator {
35+
typedef Category iterator_category;
36+
37+
iterator();
38+
iterator(iterator<Category, remove_const_t<value_type> > const &other); // non-const -> const conversion constructor
39+
40+
iterator &operator++();
41+
iterator operator++(int);
42+
iterator &operator--();
43+
iterator operator--(int);
44+
bool operator==(iterator other) const;
45+
bool operator!=(iterator other) const;
46+
reference_type operator*() const;
47+
pointer_type operator->() const;
48+
iterator operator+(int);
49+
iterator operator-(int);
50+
iterator &operator+=(int);
51+
iterator &operator-=(int);
52+
int operator-(iterator);
53+
reference_type operator[](int);
54+
};
55+
56+
struct input_iterator_tag {};
57+
struct forward_iterator_tag : public input_iterator_tag {};
58+
struct bidirectional_iterator_tag : public forward_iterator_tag {};
59+
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
60+
}
61+
62+
// --- string ---
63+
64+
namespace std
65+
{
66+
template<class charT> struct char_traits;
67+
68+
typedef size_t streamsize;
69+
70+
template <class T> class allocator {
71+
public:
72+
allocator() throw();
73+
typedef size_t size_type;
74+
};
75+
76+
template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
77+
class basic_string {
78+
public:
79+
using value_type = charT;
80+
using reference = value_type&;
81+
using const_reference = const value_type&;
82+
typedef typename Allocator::size_type size_type;
83+
static const size_type npos = -1;
84+
85+
explicit basic_string(const Allocator& a = Allocator());
86+
basic_string(const charT* s, const Allocator& a = Allocator());
87+
template<class InputIterator> basic_string(InputIterator begin, InputIterator end, const Allocator& a = Allocator());
88+
89+
const charT* c_str() const;
90+
charT* data() noexcept;
91+
size_t length() const;
92+
93+
typedef std::iterator<random_access_iterator_tag, charT> iterator;
94+
typedef std::iterator<random_access_iterator_tag, const charT> const_iterator;
95+
96+
iterator begin();
97+
iterator end();
98+
const_iterator begin() const;
99+
const_iterator end() const;
100+
const_iterator cbegin() const;
101+
const_iterator cend() const;
102+
103+
const_reference operator[](size_type pos) const;
104+
reference operator[](size_type pos);
105+
const_reference at(size_type n) const;
106+
reference at(size_type n);
107+
basic_string& insert(size_type pos, const basic_string& str);
108+
basic_string& insert(size_type pos, size_type n, charT c);
109+
basic_string& insert(size_type pos, const charT* s);
110+
iterator insert(const_iterator p, size_type n, charT c);
111+
template<class InputIterator> iterator insert(const_iterator p, InputIterator first, InputIterator last);
112+
basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
113+
basic_string& replace(size_type pos1, size_type n1, size_type n2, charT c);
114+
};
115+
116+
template<class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+(const basic_string<charT, traits, Allocator>& lhs, const basic_string<charT, traits, Allocator>& rhs);
117+
template<class charT, class traits, class Allocator> basic_string<charT, traits, Allocator> operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
118+
119+
typedef basic_string<char> string;
120+
}
121+
122+
// --- vector ---
123+
124+
namespace std {
125+
template<class T, class Allocator = allocator<T>>
126+
class vector {
127+
public:
128+
using value_type = T;
129+
using reference = value_type&;
130+
using const_reference = const value_type&;
131+
using size_type = unsigned int;
132+
using iterator = std::iterator<random_access_iterator_tag, T>;
133+
using const_iterator = std::iterator<random_access_iterator_tag, const T>;
134+
135+
vector() noexcept(noexcept(Allocator()));
136+
explicit vector(const Allocator&) noexcept;
137+
explicit vector(size_type n, const Allocator& = Allocator());
138+
vector(size_type n, const T& value, const Allocator& = Allocator());
139+
template<class InputIterator, class IteratorCategory = typename InputIterator::iterator_category> vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
140+
~vector();
141+
142+
void push_back(const T& x);
143+
void push_back(T&& x);
144+
145+
iterator insert(const_iterator position, const T& x);
146+
iterator insert(const_iterator position, T&& x);
147+
iterator insert(const_iterator position, size_type n, const T& x);
148+
template<class InputIterator> iterator insert(const_iterator position, InputIterator first, InputIterator last);
149+
150+
template <class... Args> iterator emplace (const_iterator position, Args&&... args);
151+
template <class... Args> void emplace_back (Args&&... args);
152+
};
153+
}
154+
155+
struct S {
156+
const char* s;
157+
};
158+
159+
void call_by_value(S);
160+
void call_by_cref(const S&);
161+
162+
void call(const char*);
163+
164+
const char* test(bool b1, bool b2) {
165+
auto s1 = std::string("hello").c_str(); // BAD
166+
auto s2 = b1 ? std::string("hello").c_str() : ""; // BAD
167+
auto s3 = b2 ? "" : std::string("hello").c_str(); // BAD
168+
const char* s4;
169+
s4 = std::string("hello").c_str(); // BAD
170+
171+
call(std::string("hello").c_str()); // GOOD
172+
call(b1 ? std::string("hello").c_str() : ""); // GOOD
173+
call(b1 ? (b2 ? "" : std::string("hello").c_str()) : ""); // GOOD
174+
call_by_value({ std::string("hello").c_str() }); // GOOD
175+
call_by_cref({ std::string("hello").c_str() }); // GOOD
176+
177+
std::vector<const char*> v1;
178+
v1.push_back(std::string("hello").c_str()); // BAD
179+
180+
std::vector<S> v2;
181+
v2.push_back({ std::string("hello").c_str() }); // BAD
182+
183+
S s5[] = { { std::string("hello").c_str() } }; // BAD
184+
185+
char c = std::string("hello").c_str()[0]; // GOOD
186+
187+
return std::string("hello").c_str(); // BAD
188+
}

0 commit comments

Comments
 (0)