Skip to content

Commit

Permalink
Fix double-free error.
Browse files Browse the repository at this point in the history
  • Loading branch information
grafikrobot committed Aug 16, 2023
1 parent 3ec3b11 commit 4f47a24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/engine/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ struct safe_value_cache
return result;
}

template <typename Test, typename Val>
value_ptr save(object * obj)
{
value_object test_val(obj);
std::lock_guard<std::mutex> guard(mutex);
auto existing = cache.find(&test_val);
if (existing != cache.end()) return *existing;
value_ptr result = value_object::make(test_val.value.release());
cache.insert(result);
return result;
}

void reset()
{
std::lock_guard<std::mutex> guard(mutex);
Expand Down Expand Up @@ -256,9 +268,6 @@ value_ptr value::make(double v)
return value_cache().save<value_number, value_number>(v);
}

void value::done(void)
{
value_cache().reset();
}
void value::done(void) { value_cache().reset(); }

} // namespace b2
6 changes: 6 additions & 0 deletions src/engine/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Distributed under the Boost Software License, Version 1.0.

#include "config.h"

#include "strview.h"

#include <cstdint>
#include <cstring>
#include <limits>
Expand Down Expand Up @@ -62,6 +64,10 @@ struct value
{
return make(str.c_str(), str.size());
}
static inline value * make(string_view str)
{
return make(str.data(), str.length());
}
static value * make(object * obj);
static value * make(double v);
static inline value * copy(value * v) { return v; }
Expand Down

0 comments on commit 4f47a24

Please sign in to comment.