Bug 1565627. Stop using [array] in nsIBinaryInputStream. r=froydnj
authorBoris Zbarsky <bzbarsky@mit.edu>
Tue, 16 Jul 2019 18:39:16 +0000
changeset 543493 c160562971eb15d2a3de71a011f223baedc46b3d
parent 543492 7e7807bb81551a53e4b5e03f72516e143d6980a6
child 543494 c784c14b5d5d31a0b64ab27c5e84e2ec5f164518
push id11848
push userffxbld-merge
push dateMon, 26 Aug 2019 19:26:25 +0000
treeherdermozilla-beta@9b31bfdfac10 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1565627
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1565627. Stop using [array] in nsIBinaryInputStream. r=froydnj Differential Revision: https://phabricator.services.mozilla.com/D37897
xpcom/io/nsBinaryStream.cpp
xpcom/io/nsBinaryStream.h
xpcom/io/nsIBinaryInputStream.idl
--- a/xpcom/io/nsBinaryStream.cpp
+++ b/xpcom/io/nsBinaryStream.cpp
@@ -732,44 +732,58 @@ nsBinaryInputStream::ReadString(nsAStrin
 
   if (bytesRead != length * sizeof(char16_t)) {
     return NS_ERROR_FAILURE;
   }
 
   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
 nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength,
                                      JS::Handle<JS::Value> aBuffer,
                                      JSContext* aCx, uint32_t* aReadLength) {
   if (!aBuffer.isObject()) {
     return NS_ERROR_FAILURE;
--- a/xpcom/io/nsBinaryStream.h
+++ b/xpcom/io/nsBinaryStream.h
@@ -83,13 +83,17 @@ class nsBinaryInputStream final : public
 
   // nsIObjectInputStream methods
   NS_DECL_NSIOBJECTINPUTSTREAM
 
   nsCOMPtr<nsIInputStream> mInputStream;
   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() {}
 };
 
 #endif  // nsBinaryStream_h___
--- a/xpcom/io/nsIBinaryInputStream.idl
+++ b/xpcom/io/nsIBinaryInputStream.idl
@@ -60,18 +60,17 @@ interface nsIBinaryInputStream : nsIInpu
     /**
      * Read an opaque byte array from the stream, storing the results
      * as an array of PRUint8s.
      *
      * @param aLength the number of bytes that must be read.
      *
      * @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.
      *
      * @param aLength the number of bytes that must be read
      * @param aArrayBuffer the arraybuffer in which to store the results
      * Note: passing view.buffer, where view is an ArrayBufferView of an
      *       ArrayBuffer, is not valid unless view.byteOffset == 0.