Skip to content

Commit 219a4a9

Browse files
committed
Fix memory leak when exception thrown during optimize
1 parent 348546c commit 219a4a9

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
2020
# - TE_BUILD_TEST_RUNNER to toggle running the tests
2121

2222
option(TE_BUILD_TEST_RUNNER "Build test runner" ON)
23+
option(USE_ADDRESS_SANITIZE "Use ASAN" ON)
2324

2425
project(TETestRunner)
2526

tinyexpr.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,10 +2451,20 @@ void te_parser::optimize(te_expr* texp)
24512451
}
24522452
if (known)
24532453
{
2454-
const auto value = te_eval(texp);
2455-
te_free_parameters(texp);
2456-
texp->m_type = TE_DEFAULT;
2457-
texp->m_value = value;
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+
}
24582468
}
24592469
}
24602470
}

0 commit comments

Comments
 (0)