Audio HAL: Detect buffer memory allocation failure

If the requested buffer was too big, memory allocation would fail,
resulting if a audio hal crash (uncatch exception thrown by new).

Properly hadle the failure by retuning INVALID_PARAMETERS in such case.

Bug: 36311550
Test: Run test on target

Change-Id: Ib4170eb6a9f88f9352d0912083b43d600771bb8e
Signed-off-by: Kevin Rocard <krocard@google.com>
diff --git a/audio/2.0/default/StreamIn.cpp b/audio/2.0/default/StreamIn.cpp
index 2745607..97f8307 100644
--- a/audio/2.0/default/StreamIn.cpp
+++ b/audio/2.0/default/StreamIn.cpp
@@ -20,6 +20,7 @@
 
 #include <android/log.h>
 #include <hardware/audio.h>
+#include <memory>
 #include <utils/Trace.h>
 
 #include "StreamIn.h"
@@ -52,7 +53,11 @@
               mDataMQ(dataMQ),
               mStatusMQ(statusMQ),
               mEfGroup(efGroup),
-              mBuffer(new uint8_t[dataMQ->getQuantumCount()]) {
+              mBuffer(nullptr) {
+    }
+    bool init() {
+        mBuffer.reset(new(std::nothrow) uint8_t[mDataMQ->getQuantumCount()]);
+        return mBuffer != nullptr;
     }
     virtual ~ReadThread() {}
 
@@ -328,13 +333,18 @@
     }
 
     // Create and launch the thread.
-    mReadThread = new ReadThread(
+    auto tempReadThread = std::make_unique<ReadThread>(
             &mStopReadThread,
             mStream,
             tempCommandMQ.get(),
             tempDataMQ.get(),
             tempStatusMQ.get(),
             mEfGroup);
+    if (!tempReadThread->init()) {
+        _hidl_cb(Result::INVALID_ARGUMENTS,
+                CommandMQ::Descriptor(), DataMQ::Descriptor(), StatusMQ::Descriptor(), threadInfo);
+        return Void();
+    }
     status = mReadThread->run("reader", PRIORITY_URGENT_AUDIO);
     if (status != OK) {
         ALOGW("failed to start reader thread: %s", strerror(-status));
@@ -346,6 +356,7 @@
     mCommandMQ = std::move(tempCommandMQ);
     mDataMQ = std::move(tempDataMQ);
     mStatusMQ = std::move(tempStatusMQ);
+    mReadThread = tempReadThread.release();
     threadInfo.pid = getpid();
     threadInfo.tid = mReadThread->getTid();
     _hidl_cb(Result::OK,