CPP: Add a test case involving the std::string constructor.

This commit is contained in:
Geoffrey White
2019-11-19 15:43:19 +00:00
parent 6fc9cc5952
commit fbd9d9bdab
2 changed files with 39 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
| test2.cpp:35:28:35:33 | call to malloc | This allocation does not include space to null-terminate the string. |
| test.c:16:20:16:25 | call to malloc | This allocation does not include space to null-terminate the string. | | test.c:16:20:16:25 | call to malloc | This allocation does not include space to null-terminate the string. |
| test.c:32:20:32:25 | call to malloc | This allocation does not include space to null-terminate the string. | | test.c:32:20:32:25 | call to malloc | This allocation does not include space to null-terminate the string. |
| test.c:49:20:49:25 | call to malloc | This allocation does not include space to null-terminate the string. | | test.c:49:20:49:25 | call to malloc | This allocation does not include space to null-terminate the string. |

View File

@@ -0,0 +1,38 @@
///// Library functions //////
typedef unsigned long size_t;
void *malloc(size_t size);
void free(void *ptr);
size_t strlen(const char *s);
namespace std
{
template<class charT> struct char_traits;
template <class T> class allocator {
public:
allocator() throw();
};
template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
class basic_string {
public:
explicit basic_string(const Allocator& a = Allocator());
basic_string(const charT* s, const Allocator& a = Allocator());
const charT* c_str() const;
};
typedef basic_string<char> string;
}
//// Test code /////
void bad1(char *str) {
// BAD -- Not allocating space for '\0' terminator
char *buffer = (char *)malloc(strlen(str));
std::string str2(buffer);
free(buffer);
}