From d5acf81c3ac03c00e66a6398e1ae10a4c27f08c9 Mon Sep 17 00:00:00 2001 From: Sangjung Woo Date: Thu, 26 Jan 2023 16:46:02 +0900 Subject: [PATCH] [ML][Single] Fix the crash issue in SingleShot class ml_single_close() can be called in both Close() method and destructor of SingleShot class. If the handle is invalid, calling ml_single_close() can cause the segmentation fault issue. This patch checks whether the handle is valid or not so it fixes the potential bug. Change-Id: I0ab94cae99f53d274a091acb3a21403f42664439 Signed-off-by: Sangjung Woo --- src/ml/ml_singleshot.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ml/ml_singleshot.cc b/src/ml/ml_singleshot.cc index 226c7fb7..65211ff5 100644 --- a/src/ml/ml_singleshot.cc +++ b/src/ml/ml_singleshot.cc @@ -51,9 +51,12 @@ SingleShot::SingleShot(SingleShot&& o) SingleShot::~SingleShot() { ScopeLogger("id: %d", id_); - int ret = ml_single_close(handle_); - if (ML_ERROR_NONE != ret) { - LoggerW("ml_single_close failed: %d (%s)", ret, get_error_message(ret)); + int ret = 0; + if (handle_ != nullptr) { + ret = ml_single_close(handle_); + if (ML_ERROR_NONE != ret) { + LoggerW("ml_single_close failed: %d (%s)", ret, get_error_message(ret)); + } } // not dynamic mode uses ml_single_invoke_fast, which reuses handles, so they need to be freed if (!dynamic_mode_) { @@ -263,6 +266,7 @@ PlatformResult SingleShot::Close() { return util::ToPlatformResult(ret, "Failed to close"); } + handle_ = nullptr; return PlatformResult{}; } -- 2.34.1