Bug 1565627. Stop using [array] in nsIBinaryInputStream. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D37897

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Boris Zbarsky 2019-07-16 18:39:16 +00:00
Родитель 19e0476128
Коммит cadd9a872e
3 изменённых файлов: 31 добавлений и 14 удалений

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

@ -737,34 +737,48 @@ nsBinaryInputStream::ReadString(nsAString& aString) {
return NS_OK;
}
nsresult nsBinaryInputStream::ReadBytesToBuffer(uint32_t aLength,
uint8_t* aBuffer) {
uint32_t bytesRead;
nsresult rv = Read(reinterpret_cast<char*>(aBuffer), aLength, &bytesRead);
if (NS_FAILED(rv)) {
return rv;
}
if (bytesRead != aLength) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadBytes(uint32_t aLength, char** aResult) {
nsresult rv;
uint32_t bytesRead;
char* s;
s = reinterpret_cast<char*>(malloc(aLength));
char* s = static_cast<char*>(malloc(aLength));
if (!s) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = Read(s, aLength, &bytesRead);
nsresult rv = ReadBytesToBuffer(aLength, reinterpret_cast<uint8_t*>(s));
if (NS_FAILED(rv)) {
free(s);
return rv;
}
if (bytesRead != aLength) {
free(s);
return NS_ERROR_FAILURE;
}
*aResult = s;
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadByteArray(uint32_t aLength, uint8_t** aResult) {
return ReadBytes(aLength, reinterpret_cast<char**>(aResult));
nsBinaryInputStream::ReadByteArray(uint32_t aLength,
nsTArray<uint8_t>& aResult) {
if (!aResult.SetLength(aLength, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = ReadBytesToBuffer(aLength, aResult.Elements());
if (NS_FAILED(rv)) {
aResult.Clear();
}
return rv;
}
NS_IMETHODIMP

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

@ -88,6 +88,10 @@ class nsBinaryInputStream final : public nsIObjectInputStream {
nsCOMPtr<nsIStreamBufferAccess> mBufferAccess;
private:
// Shared infrastructure for ReadBytes and ReadByteArray. Callers
// are expected to provide a buffer that can contain aLength bytes.
nsresult ReadBytesToBuffer(uint32_t aLength, uint8_t* aBuffer);
// virtual dtor since subclasses call our Release()
virtual ~nsBinaryInputStream() {}
};

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

@ -65,8 +65,7 @@ interface nsIBinaryInputStream : nsIInputStream {
*
* @throws NS_ERROR_FAILURE if it can't read aLength bytes
*/
void readByteArray(in uint32_t aLength,
[array, size_is(aLength), retval] out uint8_t aBytes);
Array<uint8_t> readByteArray(in uint32_t aLength);
/**
* Read opaque bytes from the stream, storing the results in an ArrayBuffer.