Bug 1493276 - Statically prevent CallQueryInterface to a base class r=froydnj

If a class A is derived from a class B, then an instance of A can be
converted to an instance of class B via a static cast, so QI is not
needed. QIs are slower than static casts.

TestCallTemplates seems to be testing that CallQueryInterface compiles
even if the first argument's class is only ambiguously castable to
nsISupports, so I changed the second argument to be a class unrelated
to the concrete class.

I also removed some useless null checks on the return value of new.

Differential Revision: https://phabricator.services.mozilla.com/D6838

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrew McCreight 2018-09-27 14:59:55 +00:00
parent 9ce2cbff3e
commit 5a1eb609f9
10 changed files with 37 additions and 28 deletions

View File

@ -354,7 +354,9 @@ ImageEncoder::GetInputStream(int32_t aWidth,
nsDependentString(aEncoderOptions));
NS_ENSURE_SUCCESS(rv, rv);
return CallQueryInterface(aEncoder, aStream);
nsCOMPtr<imgIEncoder> encoder(aEncoder);
encoder.forget(aStream);
return NS_OK;
}
/* static */

View File

@ -789,7 +789,7 @@ FSTextPlain::GetEncodedSubmission(nsIURI* aURI,
mimeStream->AddHeader("Content-Type", "text/plain");
mimeStream->SetData(bodyStream);
CallQueryInterface(mimeStream, aPostDataStream);
mimeStream.forget(aPostDataStream);
}
return rv;

View File

@ -3294,7 +3294,8 @@ nsHTMLDocument::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const
// State from nsHTMLDocument
clone->mLoadFlags = mLoadFlags;
return CallQueryInterface(clone.get(), aResult);
clone.forget(aResult);
return NS_OK;
}
bool

View File

@ -40,7 +40,8 @@ SVGDocument::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const
nsresult rv = CloneDocHelper(clone.get());
NS_ENSURE_SUCCESS(rv, rv);
return CallQueryInterface(clone.get(), aResult);
clone.forget(aResult);
return NS_OK;
}
} // namespace dom

View File

@ -621,7 +621,8 @@ XMLDocument::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const
clone->mAsync = mAsync;
clone->mIsPlainDocument = mIsPlainDocument;
return CallQueryInterface(clone.get(), aResult);
clone.forget(aResult);
return NS_OK;
}
JSObject*

View File

@ -1009,8 +1009,7 @@ gfxUtils::EncodeSourceSurface(SourceSurface* aSurface,
dataSurface->Unmap();
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIInputStream> imgStream;
CallQueryInterface(encoder.get(), getter_AddRefs(imgStream));
nsCOMPtr<nsIInputStream> imgStream(encoder);
if (!imgStream) {
return NS_ERROR_FAILURE;
}

View File

@ -93,12 +93,9 @@ NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult)
if (!aResult) {
return NS_ERROR_NULL_POINTER;
}
nsContentDLF* it = new nsContentDLF();
if (!it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return CallQueryInterface(it, aResult);
RefPtr<nsContentDLF> it = new nsContentDLF();
it.forget(aResult);
return NS_OK;
}
nsContentDLF::nsContentDLF()

View File

@ -1322,12 +1322,9 @@ nsWindowWatcher::GetWindowEnumerator(nsISimpleEnumerator** aResult)
}
MutexAutoLock lock(mListLock);
nsWatcherWindowEnumerator* enumerator = new nsWatcherWindowEnumerator(this);
if (enumerator) {
return CallQueryInterface(enumerator, aResult);
}
return NS_ERROR_OUT_OF_MEMORY;
RefPtr<nsWatcherWindowEnumerator> enumerator = new nsWatcherWindowEnumerator(this);
enumerator.forget(aResult);
return NS_OK;
}
NS_IMETHODIMP
@ -2094,7 +2091,8 @@ nsWindowWatcher::ReadyOpenedDocShellItem(nsIDocShellTreeItem* aOpenedItem,
doc->SetIsInitialDocument(true);
}
}
rv = CallQueryInterface(piOpenedWindow, aOpenedWindow);
piOpenedWindow.forget(aOpenedWindow);
rv = NS_OK;
}
return rv;
}
@ -2457,10 +2455,8 @@ nsWindowWatcher::GetWindowTreeItem(mozIDOMWindowProxy* aWindow,
*aResult = 0;
if (aWindow) {
nsIDocShell* docshell = nsPIDOMWindowOuter::From(aWindow)->GetDocShell();
if (docshell) {
CallQueryInterface(docshell, aResult);
}
nsCOMPtr<nsIDocShell> docshell = nsPIDOMWindowOuter::From(aWindow)->GetDocShell();
docshell.forget(aResult);
}
}

View File

@ -124,7 +124,8 @@ CallQueryInterface(T* aSource, DestinationType** aDestination)
{
// We permit nsISupports-to-nsISupports here so that one can still obtain
// the canonical nsISupports pointer with CallQueryInterface.
static_assert(!mozilla::IsSame<T, DestinationType>::value ||
static_assert(!(mozilla::IsSame<DestinationType, T>::value ||
mozilla::IsBaseOf<DestinationType, T>::value) ||
mozilla::IsSame<DestinationType, nsISupports>::value,
"don't use CallQueryInterface for compile-time-determinable casts");

View File

@ -33,6 +33,17 @@ class NS_NO_VTABLE nsITestService : public nsISupports {
NS_DEFINE_STATIC_IID_ACCESSOR(nsITestService, NS_ITESTSERVICE_IID)
#define NS_ITESTSERVICE2_IID \
{0x137b5253, 0x37b1, 0x43c7, \
{ 0x96, 0x2b, 0xab, 0xf1, 0x2d, 0x22, 0x56, 0xaf }}
class NS_NO_VTABLE nsITestService2 : public nsISupports {
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITESTSERVICE2_IID)
};
NS_DEFINE_STATIC_IID_ACCESSOR(nsITestService2, NS_ITESTSERVICE2_IID)
class nsTestService final : public nsITestService,
public nsSupportsWeakReference
{
@ -70,14 +81,14 @@ void JustTestingCompilation()
nsTestService *myTestService =
reinterpret_cast<nsTestService*>(mySupportsPtr);
nsISupportsWeakReference *mySupportsWeakRef;
CallQueryInterface(myTestService, &mySupportsWeakRef);
nsITestService2 *myTestService2;
CallQueryInterface(myTestService, &myTestService2);
nsCOMPtr<nsISupports> mySupportsCOMPtr = mySupportsPtr;
CallQueryInterface(mySupportsCOMPtr, &myITestService);
RefPtr<nsTestService> myTestServiceRefPtr = myTestService;
CallQueryInterface(myTestServiceRefPtr, &mySupportsWeakRef);
CallQueryInterface(myTestServiceRefPtr, &myTestService2);
/* Test CallQueryReferent */