Skip to content

Commit 389434d

Browse files
Merge pull request #138 from martinfantini/double_free
Double free check in template_repo_base
2 parents 41b8deb + 4528bd6 commit 389434d

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/mfast/coder/common/template_repo.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ class template_repo_base {
1414
template_repo_base(mfast::allocator *dictionary_alloc)
1515
: dictionary_alloc_(dictionary_alloc) {}
1616
virtual ~template_repo_base() {
17-
for (auto &elem : vector_enties_) {
17+
for (auto &elem : vector_entries_) {
1818
if (elem->of_array.capacity_in_bytes_)
19+
{
1920
dictionary_alloc_->deallocate(elem->of_array.content_,
2021
elem->of_array.capacity_in_bytes_);
22+
elem->of_array.capacity_in_bytes_ = 0;
23+
elem->of_array.content_ = nullptr;
24+
}
2125
}
2226
}
2327

@@ -33,17 +37,20 @@ class template_repo_base {
3337
void add_reset_entry(value_storage *entry) {
3438
reset_entries_.push_back(entry);
3539
}
40+
#ifdef TESTING_TEMPLATE_REPO_BASE
41+
public:
42+
#endif
3643
void add_vector_entry(value_storage *entry) {
3744
if (dictionary_alloc_)
38-
vector_enties_.push_back(entry);
45+
vector_entries_.push_back(entry);
3946
}
4047

4148
protected:
4249
friend class dictionary_builder;
4350

4451
typedef std::vector<value_storage *> value_entries_t;
4552
value_entries_t reset_entries_;
46-
value_entries_t vector_enties_; // for string and byteVector
53+
value_entries_t vector_entries_; // for string and byteVector
4754
arena_allocator instruction_alloc_;
4855
mfast::allocator *dictionary_alloc_;
4956
};

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ add_executable (mfast_test
6464
aggregate_view_test.cpp
6565
simple_coder_test.cpp
6666
scp_reset_test.cpp
67+
template_repo_base.cpp
6768
message_pmap_test.cpp
6869
)
6970

tests/template_repo_base.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "catch.hpp"
2+
3+
#define TESTING_TEMPLATE_REPO_BASE
4+
#include <mfast/coder/common/template_repo.h>
5+
#include <mfast/value_storage.h>
6+
#include <mfast/malloc_allocator.h>
7+
#include <mfast/instructions/template_instruction.h>
8+
#include <mfast/coder/common/dictionary_builder.h>
9+
10+
class template_repo_base_impl : public mfast::template_repo_base
11+
{
12+
public:
13+
template_repo_base_impl(mfast::allocator *dictionary_alloc)
14+
: mfast::template_repo_base(dictionary_alloc) {}
15+
16+
virtual mfast::template_instruction *get_template(uint32_t)
17+
{
18+
return nullptr;
19+
}
20+
};
21+
22+
TEST_CASE("Test template repo base with repeated pointers","[test_template_repo_base]")
23+
{
24+
using namespace mfast;
25+
26+
value_storage* storage_1_ = new value_storage();
27+
{
28+
const char * test_data = "Test_Data";
29+
storage_1_->of_array.len_ = static_cast<uint32_t>(std::strlen(test_data) + 1);
30+
storage_1_->of_array.content_ = malloc_allocator::instance()->allocate(storage_1_->of_array.len_);
31+
storage_1_->of_array.capacity_in_bytes_ = 9;
32+
}
33+
34+
{
35+
template_repo_base_impl templ_repo(malloc_allocator::instance());
36+
templ_repo.add_vector_entry(storage_1_);
37+
templ_repo.add_vector_entry(storage_1_);
38+
}
39+
40+
delete storage_1_;
41+
}

0 commit comments

Comments
 (0)