Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b9fb288

Browse filesBrowse files
committed
Fix memory leak when optimize throws
1 parent 219a4a9 commit b9fb288
Copy full SHA for b9fb288

File tree

Expand file treeCollapse file tree

1 file changed

+20
-17
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+20
-17
lines changed
Open diff view settings
Collapse file

‎tinyexpr.cpp‎

Copy file name to clipboardExpand all lines: tinyexpr.cpp
+20-17Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,20 +2451,10 @@ void te_parser::optimize(te_expr* texp)
24512451
}
24522452
if (known)
24532453
{
2454-
try
2455-
{
2456-
const auto value = te_eval(texp);
2457-
te_free_parameters(texp);
2458-
texp->m_type = TE_DEFAULT;
2459-
texp->m_value = value;
2460-
}
2461-
catch (const std::exception& exp)
2462-
{
2463-
te_free_parameters(texp);
2464-
texp->m_type = TE_DEFAULT;
2465-
texp->m_value = te_nan;
2466-
throw exp;
2467-
}
2454+
const auto value = te_eval(texp);
2455+
te_free_parameters(texp);
2456+
texp->m_type = TE_DEFAULT;
2457+
texp->m_value = value;
24682458
}
24692459
}
24702460
}
@@ -2479,6 +2469,7 @@ te_expr* te_parser::te_compile(const std::string_view expression, std::set<te_va
24792469

24802470
if (theState.m_type != state::token_type::TOK_END)
24812471
{
2472+
// if a parse error, clean up
24822473
te_free(root);
24832474
m_errorPos = (theState.m_next - theState.m_start);
24842475
if (m_errorPos > 0)
@@ -2488,7 +2479,17 @@ te_expr* te_parser::te_compile(const std::string_view expression, std::set<te_va
24882479
return nullptr;
24892480
}
24902481

2491-
optimize(root);
2482+
try
2483+
{
2484+
optimize(root);
2485+
}
2486+
catch (const std::exception& exp)
2487+
{
2488+
// parsed OK, but there was an evaluation error;
2489+
// clean up and throw the message back up to compile()
2490+
te_free(root);
2491+
throw exp;
2492+
}
24922493
m_errorPos = te_parser::npos;
24932494
return root;
24942495
}
@@ -2564,6 +2565,8 @@ bool te_parser::compile(const std::string_view expression)
25642565
m_parseSuccess = false;
25652566
m_result = te_nan;
25662567
m_lastErrorMessage = expt.what();
2568+
// not a syntax error in the expression, something threw a math error
2569+
m_errorPos = te_parser::npos;
25672570
}
25682571

25692572
reset_usr_resolved_if_necessary();
@@ -2584,11 +2587,11 @@ te_type te_parser::evaluate()
25842587
}
25852588
m_result = (m_compiledExpression != nullptr) ? te_eval(m_compiledExpression) : te_nan;
25862589
}
2587-
catch (const std::exception& except)
2590+
catch (const std::exception& exp)
25882591
{
25892592
m_parseSuccess = false;
25902593
m_result = te_nan;
2591-
m_lastErrorMessage = except.what();
2594+
m_lastErrorMessage = exp.what();
25922595
}
25932596

25942597
reset_usr_resolved_if_necessary();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.