From d594092675ca100f8ff0e8bf896e5d5edd04b022 Mon Sep 17 00:00:00 2001 From: Boik Date: Tue, 3 Oct 2017 12:42:35 +0800 Subject: [PATCH] add a null-pointer check before processing base::SupportsUserData::GetUserData --- atom/browser/api/trackable_object.cc | 18 +++++++++++------- atom/browser/net/atom_network_delegate.cc | 8 ++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/atom/browser/api/trackable_object.cc b/atom/browser/api/trackable_object.cc index 502757aa237d..fe78be40ec1c 100644 --- a/atom/browser/api/trackable_object.cc +++ b/atom/browser/api/trackable_object.cc @@ -6,6 +6,7 @@ #include "atom/browser/atom_browser_main_parts.h" #include "base/bind.h" +#include "base/memory/ptr_util.h" #include "base/supports_user_data.h" namespace mate { @@ -46,16 +47,19 @@ void TrackableObjectBase::Destroy() { } void TrackableObjectBase::AttachAsUserData(base::SupportsUserData* wrapped) { - wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_)); + wrapped->SetUserData(kTrackedObjectKey, + base::MakeUnique(weak_map_id_)); } // static -int32_t TrackableObjectBase::GetIDFromWrappedClass(base::SupportsUserData* w) { - auto id = static_cast(w->GetUserData(kTrackedObjectKey)); - if (id) - return *id; - else - return 0; +int32_t TrackableObjectBase::GetIDFromWrappedClass( + base::SupportsUserData* wrapped) { + if (wrapped) { + auto id = static_cast(wrapped->GetUserData(kTrackedObjectKey)); + if (id) + return *id; + } + return 0; } // static diff --git a/atom/browser/net/atom_network_delegate.cc b/atom/browser/net/atom_network_delegate.cc index 1b22736f9d46..5a76d966d6fd 100644 --- a/atom/browser/net/atom_network_delegate.cc +++ b/atom/browser/net/atom_network_delegate.cc @@ -82,8 +82,12 @@ void ToDictionary(base::DictionaryValue* details, net::URLRequest* request) { int frame_id = info->GetRenderFrameID(); auto* webContents = content::WebContents::FromRenderFrameHost( content::RenderFrameHost::FromID(process_id, frame_id)); - details->SetInteger("webContentsId", - atom::api::WebContents::GetIDFromWrappedClass(webContents)); + int webContentsId = atom::api::WebContents::GetIDFromWrappedClass( + webContents); + + // webContentsId must be greater than zero + if (webContentsId) + details->SetInteger("webContentsId", webContentsId); details->SetString("resourceType", ResourceTypeToString(info->GetResourceType())); } else {