Skip to content

Commit

Permalink
Bug 1415508 - use Span in constructing a byte input stream; r=mayhemer
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D20687

--HG--
extra : moz-landing-system : lando
  • Loading branch information
Alex Gaynor committed Feb 25, 2019
1 parent 0180051 commit 0e90378
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 58 deletions.
9 changes: 5 additions & 4 deletions dom/base/DOMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ already_AddRefed<Document> DOMParser::ParseFromString(const nsAString& aStr,

// The new stream holds a reference to the buffer
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), utf8str.get(),
utf8str.Length(), NS_ASSIGNMENT_DEPEND);
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), utf8str,
NS_ASSIGNMENT_DEPEND);
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return nullptr;
Expand All @@ -111,8 +111,9 @@ already_AddRefed<Document> DOMParser::ParseFromBuffer(Span<const uint8_t> aBuf,
// The new stream holds a reference to the buffer
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stream), reinterpret_cast<const char*>(aBuf.Elements()),
aBuf.Length(), NS_ASSIGNMENT_DEPEND);
getter_AddRefs(stream),
MakeSpan(reinterpret_cast<const char*>(aBuf.Elements()), aBuf.Length()),
NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
Expand Down
3 changes: 2 additions & 1 deletion dom/events/DataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,8 @@ void DataTransfer::FillInExternalCustomTypes(nsIVariant* aData, uint32_t aIndex,
}

nsCOMPtr<nsIInputStream> stringStream;
NS_NewByteInputStream(getter_AddRefs(stringStream), chrs, checkedLen.value(),
NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(chrs, checkedLen.value()),
NS_ASSIGNMENT_ADOPT);

nsCOMPtr<nsIObjectInputStream> stream = NS_NewObjectInputStream(stringStream);
Expand Down
4 changes: 2 additions & 2 deletions dom/fetch/BodyExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static nsresult GetBufferDataAsStream(
const char* data = reinterpret_cast<const char*>(aData);

nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), data, aDataLength,
NS_ASSIGNMENT_COPY);
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stream), MakeSpan(data, aDataLength), NS_ASSIGNMENT_COPY);
NS_ENSURE_SUCCESS(rv, rv);

stream.forget(aResult);
Expand Down
2 changes: 1 addition & 1 deletion dom/file/MemoryBlobImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ nsresult MemoryBlobImpl::DataOwnerAdapter::Create(DataOwner* aDataOwner,

rv = NS_NewByteInputStream(
getter_AddRefs(stream),
static_cast<const char*>(aDataOwner->mData) + aStart, (int32_t)aLength,
MakeSpan(static_cast<const char*>(aDataOwner->mData) + aStart, aLength),
NS_ASSIGNMENT_DEPEND);
NS_ENSURE_SUCCESS(rv, rv);

Expand Down
3 changes: 2 additions & 1 deletion dom/jsurl/nsJSProtocolHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ nsresult nsJSThunk::EvaluateScript(
}
aChannel->SetContentCharset(*charset);
if (bytes)
rv = NS_NewByteInputStream(getter_AddRefs(mInnerStream), bytes, bytesLen,
rv = NS_NewByteInputStream(getter_AddRefs(mInnerStream),
mozilla::MakeSpan(bytes, bytesLen),
NS_ASSIGNMENT_ADOPT);
else
rv = NS_ERROR_OUT_OF_MEMORY;
Expand Down
4 changes: 2 additions & 2 deletions dom/xhr/XMLHttpRequestMainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,8 +1542,8 @@ nsresult XMLHttpRequestMainThread::StreamReaderFunc(
// to the parser, because calling ReadSegments() recursively on the same
// stream is not supported.
nsCOMPtr<nsIInputStream> copyStream;
rv = NS_NewByteInputStream(getter_AddRefs(copyStream), fromRawSegment,
count);
rv = NS_NewByteInputStream(getter_AddRefs(copyStream),
MakeSpan(fromRawSegment, count));

if (NS_SUCCEEDED(rv) && xmlHttpRequest->mXMLParserStreamListener) {
NS_ASSERTION(copyStream, "NS_NewByteInputStream lied");
Expand Down
7 changes: 4 additions & 3 deletions gfx/thebes/gfxSVGGlyphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ gfxSVGGlyphsDocument::~gfxSVGGlyphsDocument() {
static nsresult CreateBufferedStream(const uint8_t *aBuffer, uint32_t aBufLen,
nsCOMPtr<nsIInputStream> &aResult) {
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
reinterpret_cast<const char *>(aBuffer),
aBufLen, NS_ASSIGNMENT_DEPEND);
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stream),
MakeSpan(reinterpret_cast<const char *>(aBuffer), aBufLen),
NS_ASSIGNMENT_DEPEND);
NS_ENSURE_SUCCESS(rv, rv);

nsCOMPtr<nsIInputStream> aBufferedStream;
Expand Down
4 changes: 2 additions & 2 deletions image/imgTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ imgTools::DecodeImageFromBuffer(const char* aBuffer, uint32_t aSize,

// Let's create a temporary inputStream.
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), aBuffer, aSize,
NS_ASSIGNMENT_DEPEND);
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stream), MakeSpan(aBuffer, aSize), NS_ASSIGNMENT_DEPEND);
NS_ENSURE_SUCCESS(rv, rv);
MOZ_ASSERT(stream);
MOZ_ASSERT(NS_InputStreamIsBuffered(stream));
Expand Down
4 changes: 2 additions & 2 deletions modules/libjar/zipwriter/nsDeflateConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ nsresult nsDeflateConverter::PushAvailableData(nsIRequest *aRequest,

MOZ_ASSERT(bytesToWrite <= INT32_MAX);
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
(char *)mWriteBuffer, bytesToWrite);
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stream), MakeSpan((char *)mWriteBuffer, bytesToWrite));
NS_ENSURE_SUCCESS(rv, rv);

rv = mListener->OnDataAvailable(aRequest, mContext, stream, mOffset,
Expand Down
3 changes: 2 additions & 1 deletion modules/libjar/zipwriter/nsZipDataStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ nsresult nsZipDataStream::ProcessData(nsIRequest *aRequest,

MOZ_ASSERT(aCount <= INT32_MAX);
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), aBuffer, aCount);
nsresult rv =
NS_NewByteInputStream(getter_AddRefs(stream), MakeSpan(aBuffer, aCount));
NS_ENSURE_SUCCESS(rv, rv);

rv = mOutput->OnDataAvailable(aRequest, aContext, stream, aOffset, aCount);
Expand Down
5 changes: 3 additions & 2 deletions netwerk/protocol/ftp/FTPChannelChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ void FTPChannelChild::DoOnDataAvailable(const nsresult& channelStatus,
// support only reading part of the data, allowing later calls to read the
// rest.
nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(),
count, NS_ASSIGNMENT_DEPEND);
nsresult rv =
NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(data).To(count), NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
Cancel(rv);
return;
Expand Down
5 changes: 3 additions & 2 deletions netwerk/protocol/ftp/FTPChannelParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ void FTPChannelParent::DivertOnDataAvailable(const nsCString& data,
}

nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(),
count, NS_ASSIGNMENT_DEPEND);
nsresult rv =
NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(data).To(count), NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
if (mChannel) {
mChannel->Cancel(rv);
Expand Down
5 changes: 3 additions & 2 deletions netwerk/protocol/http/HttpChannelChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,9 @@ void HttpChannelChild::OnTransportAndData(const nsresult& channelStatus,
// support only reading part of the data, allowing later calls to read the
// rest.
nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(),
count, NS_ASSIGNMENT_DEPEND);
nsresult rv =
NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(data).To(count), NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
Cancel(rv);
return;
Expand Down
5 changes: 3 additions & 2 deletions netwerk/protocol/http/HttpChannelParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,9 @@ void HttpChannelParent::DivertOnDataAvailable(const nsCString& data,
}

nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(),
count, NS_ASSIGNMENT_DEPEND);
nsresult rv =
NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(data).To(count), NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
if (mChannel) {
mChannel->Cancel(rv);
Expand Down
3 changes: 1 addition & 2 deletions netwerk/protocol/http/nsHttpTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@ nsresult nsHttpTransaction::Init(
// a non-owning reference to the request header data, so we MUST keep
// mReqHeaderBuf around).
nsCOMPtr<nsIInputStream> headers;
rv = NS_NewByteInputStream(getter_AddRefs(headers), mReqHeaderBuf.get(),
mReqHeaderBuf.Length());
rv = NS_NewByteInputStream(getter_AddRefs(headers), mReqHeaderBuf);
if (NS_FAILED(rv)) return rv;

mHasRequestBody = !!requestBody;
Expand Down
4 changes: 2 additions & 2 deletions netwerk/protocol/wyciwyg/WyciwygChannelChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ void WyciwygChannelChild::OnDataAvailable(const nsCString& data,
// support only reading part of the data, allowing later calls to read the
// rest.
nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(),
data.Length(), NS_ASSIGNMENT_DEPEND);
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data,
NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
Cancel(rv);
return;
Expand Down
7 changes: 4 additions & 3 deletions security/manager/ssl/nsNSSCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ OCSPRequest::Run() {
}

nsCOMPtr<nsIInputStream> uploadStream;
rv = NS_NewByteInputStream(getter_AddRefs(uploadStream),
reinterpret_cast<const char*>(mPOSTData.begin()),
mPOSTData.length());
rv = NS_NewByteInputStream(
getter_AddRefs(uploadStream),
MakeSpan(reinterpret_cast<const char*>(mPOSTData.begin()),
mPOSTData.length()));
if (NS_FAILED(rv)) {
return NotifyDone(rv, lock);
}
Expand Down
5 changes: 3 additions & 2 deletions startupcache/StartupCacheUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ namespace scache {
nsresult NewObjectInputStreamFromBuffer(UniquePtr<char[]> buffer, uint32_t len,
nsIObjectInputStream **stream) {
nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stringStream), buffer.release(), len, NS_ASSIGNMENT_ADOPT);
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(buffer.release(), len),
NS_ASSIGNMENT_ADOPT);
MOZ_ALWAYS_SUCCEEDS(rv);

nsCOMPtr<nsIObjectInputStream> objectInput =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ nsresult StreamFilterParent::Write(Data& aData) {
AssertIsIOThread();

nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
reinterpret_cast<char*>(aData.Elements()),
aData.Length());
nsresult rv = NS_NewByteInputStream(
getter_AddRefs(stream),
MakeSpan(reinterpret_cast<char*>(aData.Elements()), aData.Length()));
NS_ENSURE_SUCCESS(rv, rv);

rv = mOrigListener->OnDataAvailable(mChannel, mContext, stream, mOffset,
Expand Down
4 changes: 2 additions & 2 deletions toolkit/components/places/FaviconHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ nsresult FetchAndConvertUnsupportedPayloads::ConvertPayload(

// Convert the payload to an input stream.
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), aPayload.get(),
aPayload.Length(), NS_ASSIGNMENT_DEPEND);
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), aPayload,
NS_ASSIGNMENT_DEPEND);
NS_ENSURE_SUCCESS(rv, rv);

// Decode the input stream to a surface.
Expand Down
6 changes: 3 additions & 3 deletions tools/fuzzing/interface/FuzzingInterfaceStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ void afl_interface_stream(const char* testFile, FuzzingTestFuncStream testFunc);
static int LibFuzzerTest##moduleName(const uint8_t* data, size_t size) { \
if (size > INT32_MAX) return 0; \
nsCOMPtr<nsIInputStream> stream; \
nsresult rv = \
NS_NewByteInputStream(getter_AddRefs(stream), (const char*)data, \
size, NS_ASSIGNMENT_DEPEND); \
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), \
MakeSpan((const char*)data, size), \
NS_ASSIGNMENT_DEPEND); \
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); \
testFunc(stream.forget()); \
return 0; \
Expand Down
5 changes: 3 additions & 2 deletions uriloader/exthandler/ExternalHelperAppParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ mozilla::ipc::IPCResult ExternalHelperAppParent::RecvOnDataAvailable(
MOZ_ASSERT(mPending, "must be pending!");

nsCOMPtr<nsIInputStream> stringStream;
DebugOnly<nsresult> rv = NS_NewByteInputStream(
getter_AddRefs(stringStream), data.get(), count, NS_ASSIGNMENT_DEPEND);
DebugOnly<nsresult> rv =
NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(data).To(count), NS_ASSIGNMENT_DEPEND);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create dependent string!");
mStatus =
mListener->OnDataAvailable(this, nullptr, stringStream, offset, count);
Expand Down
6 changes: 4 additions & 2 deletions widget/cocoa/nsClipboard.mm
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,10 @@ + (NSString*)stringFromPboardType:(NSString*)aType {
if (successfullyConverted) {
// Put the converted data in a form Gecko can understand
nsCOMPtr<nsIInputStream> byteStream;
NS_NewByteInputStream(getter_AddRefs(byteStream), (const char*)[encodedData bytes],
[encodedData length], NS_ASSIGNMENT_COPY);
NS_NewByteInputStream(
getter_AddRefs(byteStream),
mozilla::MakeSpan((const char*)[encodedData bytes], [encodedData length]),
NS_ASSIGNMENT_COPY);

aTransferable->SetTransferData(flavorStr.get(), byteStream);
}
Expand Down
5 changes: 3 additions & 2 deletions widget/gtk/nsClipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ nsClipboard::GetData(nsITransferable *aTransferable, int32_t aWhichClipboard) {
if (!clipboardData) continue;

nsCOMPtr<nsIInputStream> byteStream;
NS_NewByteInputStream(getter_AddRefs(byteStream), clipboardData,
clipboardDataLength, NS_ASSIGNMENT_COPY);
NS_NewByteInputStream(getter_AddRefs(byteStream),
MakeSpan(clipboardData, clipboardDataLength),
NS_ASSIGNMENT_COPY);
aTransferable->SetTransferData(flavorStr.get(), byteStream);

mContext->ReleaseClipboardData(clipboardData);
Expand Down
9 changes: 5 additions & 4 deletions xpcom/io/nsStringStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ nsStringInputStream::Clone(nsIInputStream** aCloneOut) {
}

nsresult NS_NewByteInputStream(nsIInputStream** aStreamResult,
const char* aStringToRead, int32_t aLength,
Span<const char> aStringToRead,
nsAssignmentType aAssignment) {
MOZ_ASSERT(aStreamResult, "null out ptr");

Expand All @@ -453,13 +453,14 @@ nsresult NS_NewByteInputStream(nsIInputStream** aStreamResult,
nsresult rv;
switch (aAssignment) {
case NS_ASSIGNMENT_COPY:
rv = stream->SetData(aStringToRead, aLength);
rv = stream->SetData(aStringToRead.Elements(), aStringToRead.Length());
break;
case NS_ASSIGNMENT_DEPEND:
rv = stream->ShareData(aStringToRead, aLength);
rv = stream->ShareData(aStringToRead.Elements(), aStringToRead.Length());
break;
case NS_ASSIGNMENT_ADOPT:
rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
rv = stream->AdoptData(const_cast<char*>(aStringToRead.Elements()),
aStringToRead.Length());
break;
default:
NS_ERROR("invalid assignment type");
Expand Down
4 changes: 2 additions & 2 deletions xpcom/io/nsStringStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
* determined by scanning the buffer for the first null byte.
*/
extern nsresult NS_NewByteInputStream(
nsIInputStream** aStreamResult, const char* aStringToRead,
int32_t aLength = -1, nsAssignmentType aAssignment = NS_ASSIGNMENT_DEPEND);
nsIInputStream** aStreamResult, mozilla::Span<const char> aStringToRead,
nsAssignmentType aAssignment = NS_ASSIGNMENT_DEPEND);

/**
* Factory method to get an nsInputStream from an nsACString. Result will
Expand Down
4 changes: 2 additions & 2 deletions xpcom/tests/gtest/TestSnappyStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ static void TestCompressUncompress(uint32_t aNumBytes) {
static void TestUncompressCorrupt(const char* aCorruptData,
uint32_t aCorruptLength) {
nsCOMPtr<nsIInputStream> source;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(source), aCorruptData,
aCorruptLength);
nsresult rv = NS_NewByteInputStream(getter_AddRefs(source),
MakeSpan(aCorruptData, aCorruptLength));
ASSERT_TRUE(NS_SUCCEEDED(rv));

nsCOMPtr<nsIInputStream> uncompress = new SnappyUncompressInputStream(source);
Expand Down

0 comments on commit 0e90378

Please sign in to comment.