Bug 1300476 - Prevent passing references through InvokeAsync - r=froydnj

Passing references to an async call is dangerous because referenced objects
could be destroyed before/during the call, or even be stored by the callee.

The assertion message points at bug 1313497, which is a follow-up to
(eventually) re-allow references in a safer manner.

MozReview-Commit-ID: FTgI5CGCVAe

--HG--
extra : rebase_source : 3062a7441e21617f559accf4476cdafa5575e8ed
This commit is contained in:
Gerald Squelart 2016-10-27 15:13:37 +11:00
parent 38997c63a1
commit a9a4de3944

View File

@ -14,6 +14,7 @@
#include "mozilla/Mutex.h"
#include "mozilla/Monitor.h"
#include "mozilla/Tuple.h"
#include "mozilla/TypeTraits.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
@ -968,6 +969,23 @@ private:
nsAutoPtr<MethodCall<PromiseType, ThisType, ArgTypes...>> mMethodCall;
};
constexpr bool Any()
{
return false;
}
template <typename T1>
constexpr bool Any(T1 a)
{
return static_cast<bool>(a);
}
template <typename T1, typename... Ts>
constexpr bool Any(T1 a, Ts... aOthers)
{
return a || Any(aOthers...);
}
} // namespace detail
template<typename PromiseType, typename ThisType, typename ...ArgTypes, typename ...ActualArgTypes>
@ -975,6 +993,8 @@ static RefPtr<PromiseType>
InvokeAsync(AbstractThread* aTarget, ThisType* aThisVal, const char* aCallerName,
RefPtr<PromiseType>(ThisType::*aMethod)(ArgTypes...), ActualArgTypes&&... aArgs)
{
static_assert(!detail::Any(IsReference<ArgTypes>::value...),
"Cannot pass reference types through InvokeAsync, see bug 1313497 if you require it");
typedef detail::MethodCall<PromiseType, ThisType, ArgTypes...> MethodCallType;
typedef detail::ProxyRunnable<PromiseType, ThisType, ArgTypes...> ProxyRunnableType;