diff --git a/xpcom/io/nsStreamUtils.cpp b/xpcom/io/nsStreamUtils.cpp index e38c037302e7..e23cda49e07a 100644 --- a/xpcom/io/nsStreamUtils.cpp +++ b/xpcom/io/nsStreamUtils.cpp @@ -853,6 +853,10 @@ nsresult NS_CloneInputStream(nsIInputStream* aSource, nsIInputStream** aCloneOut, nsIInputStream** aReplacementOut) { + if (NS_WARN_IF(!aSource)) { + return NS_ERROR_FAILURE; + } + // Attempt to perform the clone directly on the source stream nsCOMPtr cloneable = do_QueryInterface(aSource); if (cloneable && cloneable->GetCloneable()) { diff --git a/xpcom/tests/gtest/TestCloneInputStream.cpp b/xpcom/tests/gtest/TestCloneInputStream.cpp index d9b2dc28dde2..633d207988f9 100644 --- a/xpcom/tests/gtest/TestCloneInputStream.cpp +++ b/xpcom/tests/gtest/TestCloneInputStream.cpp @@ -12,6 +12,14 @@ #include "nsStreamUtils.h" #include "nsStringStream.h" +TEST(CloneInputStream, InvalidInput) +{ + nsCOMPtr clone; + nsresult rv = NS_CloneInputStream(nullptr, getter_AddRefs(clone)); + ASSERT_TRUE(NS_FAILED(rv)); + ASSERT_FALSE(clone); +} + TEST(CloneInputStream, CloneableInput) { nsTArray inputData;