[ML][Single] Fix the crash issue in SingleShot class 45/287545/1 accepted/tizen/7.0/unified/20230201.173115 accepted/tizen/7.0/unified/20230208.184457 accepted/tizen/7.0/unified/20230208.231224 accepted/tizen/7.0/unified/20230208.231243
authorSangjung Woo <sangjung.woo@samsung.com>
Thu, 26 Jan 2023 07:46:02 +0000 (16:46 +0900)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 31 Jan 2023 07:46:11 +0000 (07:46 +0000)
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>
(cherry picked from commit d5acf81c3ac03c00e66a6398e1ae10a4c27f08c9)

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{};
 }