Skip to content

Commit

Permalink
Fix memleak during WFL parsing (fixes #1680, fixes #1685)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed May 11, 2017
1 parent 33792a3 commit 7b13d6c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
23 changes: 4 additions & 19 deletions src/formula/formula.cpp
Expand Up @@ -68,13 +68,9 @@ const char* const formula::id_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO
formula::formula(const std::string& text, function_symbol_table* symbols)
: expr_()
, str_(text)
, symbols_(symbols)
, managing_symbols(symbols == nullptr)
, managed_symbols_(symbols ? nullptr : new function_symbol_table)
, symbols_(symbols ? symbols : managed_symbols_.get())
{
if(symbols == nullptr) {
symbols_ = new function_symbol_table;
}

std::vector<tk::token> tokens;
std::string::const_iterator i1 = text.begin(), i2 = text.end();

Expand Down Expand Up @@ -213,27 +209,16 @@ formula::formula(const std::string& text, function_symbol_table* symbols)
formula::formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols)
: expr_()
, str_()
, symbols_(symbols)
, managing_symbols(symbols == nullptr)
, managed_symbols_(symbols ? nullptr : new function_symbol_table)
, symbols_(symbols ? symbols : managed_symbols_.get())
{
if(symbols == nullptr) {
symbols_ = new function_symbol_table;
}

if(i1 != i2) {
expr_ = parse_expression(i1, i2, symbols);
} else {
expr_ = expression_ptr(new null_expression());
}
}

formula::~formula()
{
if(managing_symbols) {
delete symbols_;
}
}

formula_ptr formula::create_optional_formula(const std::string& str, function_symbol_table* symbols)
{
if(str.empty()) {
Expand Down
7 changes: 4 additions & 3 deletions src/formula/formula.hpp
Expand Up @@ -18,6 +18,7 @@
#include "formula/formula_fwd.hpp"
#include "formula/tokenizer.hpp"
#include "formula/variant.hpp"
#include <memory>

namespace wfl
{
Expand All @@ -35,8 +36,6 @@ class formula
formula(const std::string& str, function_symbol_table* symbols = nullptr);
formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols = nullptr);

~formula();

static variant evaluate(
const const_formula_ptr& f,
const formula_callable& variables,
Expand Down Expand Up @@ -80,8 +79,10 @@ class formula

expression_ptr expr_;
std::string str_;
// Can't be a unique_ptr because function_symbol_table is an incomplete type,
// and the header it's declared in depends on this one.
const std::shared_ptr<function_symbol_table> managed_symbols_;
function_symbol_table* symbols_;
bool managing_symbols;

friend class formula_debugger;
};
Expand Down

0 comments on commit 7b13d6c

Please sign in to comment.