[WebAI] Fix state check issue 59/318959/2
authorpeng.yin <peng8.yin@samsung.com>
Fri, 11 Oct 2024 08:06:13 +0000 (16:06 +0800)
committerBot Blink <blinkbot@samsung.com>
Fri, 11 Oct 2024 10:04:36 +0000 (10:04 +0000)
fix the imcomplete state checking logic which leads to a nullptr
dereference issue.

Change-Id: I0a45fe89d8654493e6826462f408f91e69779c54
Signed-off-by: peng.yin <peng8.yin@samsung.com>
tizen_src/chromium_impl/webai/content/ai_processor_video_impl.cc
tizen_src/chromium_impl/webai/content/ai_processor_video_impl.h

index 021efec8096bf355e3d741ea3bb5d73bfa4e90cc..a1eebedaf8923f61f38319cc2fda1e5eff72505c 100644 (file)
@@ -47,17 +47,18 @@ AiProcessorVideoImpl::AiProcessorVideoImpl(
   xr_adapter_h handle{};
   CheckResult(xr_adapter_create(&handle), AIFW_RESULT_SUCCESS, false);
   adapter_.reset(handle);
+  state_ = AdapterState::kStopped;
 }
 
 AiProcessorVideoImpl::~AiProcessorVideoImpl() {
   AI_LOG(INFO) << __func__;
+  state_ = AdapterState::kNone;
 }
 
 void AiProcessorVideoImpl::ProcessVideo(
     const scoped_refptr<media::VideoFrame>& video_frame) {
   TRACE_EVENT0("webai", "AiProcessorVideoImpl::ProcessVideo");
-
-  if (!is_ready_) {
+  if (state_ != AdapterState::kStarted) {
     AI_LOG(ERROR) << "Configurate it first.";
     return;
   }
@@ -120,8 +121,8 @@ void AiProcessorVideoImpl::Start(const blink::AiConfiguration& configure) {
                << " fps:" << static_cast<int>(configure.fps)
                << " output_width:" << configure.output_width
                << " output_height:" << configure.output_height;
-  if (!adapter_) {
-    AI_LOG(ERROR) << "There is no adapter.";
+  if (state_ != AdapterState::kStopped) {
+    AI_LOG(ERROR) << "Invalid state:" << static_cast<int>(state_);
     return;
   }
 
@@ -142,17 +143,23 @@ void AiProcessorVideoImpl::Start(const blink::AiConfiguration& configure) {
   }
   cfg_ = configure;
   buffer_pool_ = base::MakeRefCounted<AiVideoFrameBufferPool>();
-  is_ready_ = true;
+  state_ = AdapterState::kStarted;
 }
 
 void AiProcessorVideoImpl::Stop() {
   TRACE_EVENT0("webai", "AiProcessorVideoImpl::Start");
   AI_LOG(INFO) << __func__;
+  if (state_ != AdapterState::kStarted) {
+    AI_LOG(ERROR) << "Invalid state:" << static_cast<int>(state_);
+    return;
+  }
+
   CheckResult(xr_adapter_deinit(adapter_.get()), AIFW_RESULT_SUCCESS, true);
 
   // Release all buffers.
   constexpr gfx::Size max_size{4096, 4096};
   buffer_pool_->FlushPool(max_size);
+  state_ = AdapterState::kStopped;
 }
 
 scoped_refptr<AiVideoFrameBuffer> AiProcessorVideoImpl::FindReusableBuffer(
index c99e81913af7f046082cff76a55f897cc986edd0..e39a29a1329a25f2cb7f1f5e0a95652cafef20d9 100644 (file)
@@ -51,6 +51,11 @@ class CONTENT_EXPORT AiProcessorVideoImpl final
   auto const& get_client() const { return client_; }
 
  private:
+  enum class AdapterState {
+    kNone,
+    kStarted,
+    kStopped,
+  };
   struct AdapterDeleter {
     void operator()(xr_adapter_h adapter);
   };
@@ -59,7 +64,7 @@ class CONTENT_EXPORT AiProcessorVideoImpl final
 
   absl::optional<std::string> config_json_;
   blink::AiConfiguration cfg_;
-  bool is_ready_{false};
+  AdapterState state_{};
   scoped_refptr<AiVideoFrameBufferPool> buffer_pool_;
   std::unique_ptr<std::remove_pointer<xr_adapter_h>::type, AdapterDeleter>
       adapter_;