Skip to content

Commit

Permalink
Ban const-qualified raw pointers to ref-counted objects on base::Bind…
Browse files Browse the repository at this point in the history
… parameter

base::Bind is intended to reject raw pointers to ref-counted objects,
however, it fails to reject CVR qualified ones.

This CL fixes the check for const-qualified cases.

Bug: 737010
Change-Id: I0a3c51d36a240257c66682a2fe061dc514e40835
Reviewed-on: https://chromium-review.googlesource.com/558594
Reviewed-by: Olga Sharonova <olka@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485212}
  • Loading branch information
tzik authored and Commit Bot committed Jul 10, 2017
1 parent 0729d30 commit f41b933
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
18 changes: 18 additions & 0 deletions base/bind_unittest.nc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ void WontCompile() {
Bind(&VoidPolymorphic1<HasRef*>, for_raw_ptr);
}

#elif defined(NCTEST_NO_RVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed \"A parameter is a refcounted type and needs scoped_refptr.\""]

// Refcounted types should not be bound as a raw pointer.
void WontCompile() {
const HasRef for_raw_ptr;
Callback<void()> ref_count_as_raw_ptr =
Bind(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr);
}

#elif defined(NCTEST_NO_LVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed \"A parameter is a refcounted type and needs scoped_refptr.\""]

// Refcounted types should not be bound as a raw pointer.
void WontCompile() {
const HasRef* for_raw_ptr = nullptr;
Callback<void()> ref_count_as_raw_ptr =
Bind(&VoidPolymorphic1<const HasRef*>, for_raw_ptr);
}

#elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_assert failed \"weak_ptrs can only bind to methods without return values\""]

// WeakPtrs cannot be bound to methods with return types.
Expand Down
8 changes: 5 additions & 3 deletions base/memory/raw_scoped_refptr_mismatch_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ struct NeedsScopedRefptrButGetsRawPtr {
// Human readable translation: you needed to be a scoped_refptr if you are a
// raw pointer type and are convertible to a RefCounted(Base|ThreadSafeBase)
// type.
value = (std::is_pointer<T>::value &&
(std::is_convertible<T, subtle::RefCountedBase*>::value ||
std::is_convertible<T, subtle::RefCountedThreadSafeBase*>::value))
value =
(std::is_pointer<T>::value &&
(std::is_convertible<T, const subtle::RefCountedBase*>::value ||
std::is_convertible<T,
const subtle::RefCountedThreadSafeBase*>::value))
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,6 @@ IN_PROC_BROWSER_TEST_F(BrowserWindowPropertyManagerTest, DISABLED_HostedApp) {
ASSERT_TRUE(app_browser != browser());

WaitAndValidateBrowserWindowProperties(
base::Bind(&ValidateHostedAppWindowProperties, app_browser, extension));
base::Bind(&ValidateHostedAppWindowProperties, app_browser,
base::RetainedRef(extension)));
}

0 comments on commit f41b933

Please sign in to comment.