mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 19:35:51 +00:00
Bug 1437152 - NS_ReadInputStreamToString doesn't reallocate the string if the size is passed, r=froydnj
This commit is contained in:
parent
e4bb1bbe71
commit
fd3aec925b
@ -1874,11 +1874,30 @@ NS_ReadInputStreamToString(nsIInputStream* aInputStream,
|
||||
aWritten = &dummyWritten;
|
||||
}
|
||||
|
||||
// Nothing to do if aCount is 0.
|
||||
if (aCount == 0) {
|
||||
aDest.Truncate();
|
||||
*aWritten = 0;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// If we have the size, we can pre-allocate the buffer.
|
||||
if (aCount > 0) {
|
||||
if (NS_WARN_IF(aCount >= INT32_MAX) ||
|
||||
NS_WARN_IF(!aDest.SetLength(aCount, mozilla::fallible))) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
void* dest = aDest.BeginWriting();
|
||||
return NS_ReadInputStreamToBuffer(aInputStream, &dest, aCount,
|
||||
aWritten);
|
||||
}
|
||||
|
||||
// If the size is unknown, BufferWriter will allocate the buffer.
|
||||
void* dest = nullptr;
|
||||
nsresult rv = NS_ReadInputStreamToBuffer(aInputStream, &dest, aCount,
|
||||
aWritten);
|
||||
MOZ_ASSERT_IF(NS_FAILED(rv), dest == nullptr);
|
||||
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
if (!dest) {
|
||||
|
@ -3,6 +3,28 @@
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsNetUtil.h"
|
||||
|
||||
// Here we test the reading a pre-allocated size
|
||||
TEST(TestReadStreamToString, SyncStreamPreAllocatedSize) {
|
||||
nsCString buffer;
|
||||
buffer.AssignLiteral("Hello world!");
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
ASSERT_EQ(NS_OK, NS_NewCStringInputStream(getter_AddRefs(stream), buffer));
|
||||
|
||||
uint64_t written;
|
||||
nsAutoCString result;
|
||||
result.SetLength(5);
|
||||
|
||||
void* ptr = result.BeginWriting();
|
||||
|
||||
ASSERT_EQ(NS_OK, NS_ReadInputStreamToString(stream, result, 5, &written));
|
||||
ASSERT_EQ((uint64_t)5, written);
|
||||
ASSERT_TRUE(nsCString(buffer.get(), 5).Equals(result));
|
||||
|
||||
// The pointer should be equal: no relocation.
|
||||
ASSERT_EQ(ptr, result.BeginWriting());
|
||||
}
|
||||
|
||||
// Here we test the reading the full size of a sync stream
|
||||
TEST(TestReadStreamToString, SyncStreamFullSize) {
|
||||
nsCString buffer;
|
||||
|
Loading…
Reference in New Issue
Block a user