Skip to content

Commit fbd9d9b

Browse files
committed
CPP: Add a test case involving the std::string constructor.
1 parent 6fc9cc5 commit fbd9d9b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/NoSpaceForZeroTerminator.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| test2.cpp:35:28:35:33 | call to malloc | This allocation does not include space to null-terminate the string. |
12
| test.c:16:20:16:25 | call to malloc | This allocation does not include space to null-terminate the string. |
23
| test.c:32:20:32:25 | call to malloc | This allocation does not include space to null-terminate the string. |
34
| test.c:49:20:49:25 | call to malloc | This allocation does not include space to null-terminate the string. |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
///// Library functions //////
3+
4+
typedef unsigned long size_t;
5+
6+
void *malloc(size_t size);
7+
void free(void *ptr);
8+
size_t strlen(const char *s);
9+
10+
namespace std
11+
{
12+
template<class charT> struct char_traits;
13+
14+
template <class T> class allocator {
15+
public:
16+
allocator() throw();
17+
};
18+
19+
template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
20+
class basic_string {
21+
public:
22+
explicit basic_string(const Allocator& a = Allocator());
23+
basic_string(const charT* s, const Allocator& a = Allocator());
24+
25+
const charT* c_str() const;
26+
};
27+
28+
typedef basic_string<char> string;
29+
}
30+
31+
//// Test code /////
32+
33+
void bad1(char *str) {
34+
// BAD -- Not allocating space for '\0' terminator
35+
char *buffer = (char *)malloc(strlen(str));
36+
std::string str2(buffer);
37+
free(buffer);
38+
}

0 commit comments

Comments
 (0)