Bug 1171208 - Fix null-terminating chunks in ChunkedJSONWriteFunc. (r=mstange)

This commit is contained in:
Shu-yu Guo 2015-06-04 03:22:56 -07:00
parent 16fb1bcafd
commit 671ac78eba

View File

@ -12,6 +12,7 @@ ChunkedJSONWriteFunc::Write(const char* aStr)
{
MOZ_ASSERT(mChunkPtr >= mChunkList.back().get() && mChunkPtr <= mChunkEnd);
MOZ_ASSERT(mChunkEnd >= mChunkList.back().get() + mChunkLengths.back());
MOZ_ASSERT(*mChunkPtr == '\0');
size_t len = strlen(aStr);
@ -20,17 +21,18 @@ ChunkedJSONWriteFunc::Write(const char* aStr)
// than a chunk, allocate its own chunk.
char* newPtr;
if (len >= kChunkSize) {
AllocChunk(len);
AllocChunk(len + 1);
newPtr = mChunkPtr + len;
} else {
newPtr = mChunkPtr + len;
if (newPtr > mChunkEnd) {
if (newPtr >= mChunkEnd) {
AllocChunk(kChunkSize);
newPtr = mChunkPtr + len;
}
}
memcpy(mChunkPtr, aStr, len);
*newPtr = '\0';
mChunkPtr = newPtr;
mChunkLengths.back() += len;
}
@ -62,6 +64,7 @@ ChunkedJSONWriteFunc::AllocChunk(size_t aChunkSize)
mozilla::UniquePtr<char[]> newChunk = mozilla::MakeUnique<char[]>(aChunkSize);
mChunkPtr = newChunk.get();
mChunkEnd = mChunkPtr + aChunkSize;
*mChunkPtr = '\0';
MOZ_ALWAYS_TRUE(mChunkLengths.append(0));
MOZ_ALWAYS_TRUE(mChunkList.append(mozilla::Move(newChunk)));
}