[ML][Single] Fix the crash issue in SingleShot class 43/287543/1 accepted/tizen/unified/20230202.015309
authorSangjung Woo <sangjung.woo@samsung.com>
Thu, 26 Jan 2023 07:46:02 +0000 (16:46 +0900)
committerSangjung Woo <sangjung.woo@samsung.com>
Tue, 31 Jan 2023 06:52:52 +0000 (15:52 +0900)
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 <sangjung.woo@samsung.com>
src/ml/ml_singleshot.cc

index 226c7fb..65211ff 100644 (file)
@@ -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{};
 }