Bug 965362 - Part 1: Make sure that nsScriptableInputStream::Read reads all of the allowed available bytes from the input stream; r=bsmedberg

This commit is contained in:
Ehsan Akhgari 2014-03-27 11:20:08 -04:00
Родитель 5e4435640e
Коммит 8a08eed87d
2 изменённых файлов: 15 добавлений и 5 удалений

Просмотреть файл

@ -45,14 +45,13 @@ nsScriptableInputStream::Read(uint32_t aCount, char **_retval) {
buffer = (char*)moz_malloc(count+1); // make room for '\0' buffer = (char*)moz_malloc(count+1); // make room for '\0'
if (!buffer) return NS_ERROR_OUT_OF_MEMORY; if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
uint32_t amtRead = 0; rv = ReadHelper(buffer, count);
rv = mInputStream->Read(buffer, count, &amtRead);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
nsMemory::Free(buffer); nsMemory::Free(buffer);
return rv; return rv;
} }
buffer[amtRead] = '\0'; buffer[count] = '\0';
*_retval = buffer; *_retval = buffer;
return NS_OK; return NS_OK;
} }
@ -69,10 +68,20 @@ nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString &_retval) {
} }
char *ptr = _retval.BeginWriting(); char *ptr = _retval.BeginWriting();
nsresult rv = ReadHelper(ptr, aCount);
if (NS_FAILED(rv)) {
_retval.Truncate();
}
return rv;
}
nsresult
nsScriptableInputStream::ReadHelper(char* aBuffer, uint32_t aCount)
{
uint32_t totalBytesRead = 0; uint32_t totalBytesRead = 0;
while (1) { while (1) {
uint32_t bytesRead; uint32_t bytesRead;
nsresult rv = mInputStream->Read(ptr + totalBytesRead, nsresult rv = mInputStream->Read(aBuffer + totalBytesRead,
aCount - totalBytesRead, aCount - totalBytesRead,
&bytesRead); &bytesRead);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -86,7 +95,6 @@ nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString &_retval) {
// If we have read zero bytes, we have hit EOF. // If we have read zero bytes, we have hit EOF.
if (bytesRead == 0) { if (bytesRead == 0) {
_retval.Truncate();
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }

Просмотреть файл

@ -33,6 +33,8 @@ public:
private: private:
~nsScriptableInputStream() {} ~nsScriptableInputStream() {}
nsresult ReadHelper(char* aBuffer, uint32_t aCount);
nsCOMPtr<nsIInputStream> mInputStream; nsCOMPtr<nsIInputStream> mInputStream;
}; };