Merge cherrypicks of ['googleplex-android-review.googlesource.com/23219878', 'googleplex-android-review.googlesource.com/23219026'] into tm-qpr3-c-release.

Change-Id: Ie0afabcb4c2321cc0637d1cd400bda5fac2a9a4e
diff --git a/shim_and_sl/ShimConverter.cpp b/shim_and_sl/ShimConverter.cpp
index 2cbdc09..1ed0e31 100644
--- a/shim_and_sl/ShimConverter.cpp
+++ b/shim_and_sl/ShimConverter.cpp
@@ -128,6 +128,12 @@
 
         switch (operand.lifetime) {
             case OperandLifeTime::CONSTANT_COPY: {
+                if (operand.location.length + operand.location.offset >
+                    model.operandValues.size()) {
+                    *errorStatus = ErrorStatus::INVALID_ARGUMENT;
+                    return nullptr;
+                }
+
                 if (operand.location.length <=
                     ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES) {
                     resultModel.setOperandValue(
diff --git a/shim_and_sl/ShimPreparedModel.cpp b/shim_and_sl/ShimPreparedModel.cpp
index 178cc1c..840d65a 100644
--- a/shim_and_sl/ShimPreparedModel.cpp
+++ b/shim_and_sl/ShimPreparedModel.cpp
@@ -85,6 +85,11 @@
     }
 
     const auto& model = mMainAndReferencedModels[0];
+
+    if (request.inputs.size() > model.getInputs().size()) {
+        return ErrorStatus::INVALID_ARGUMENT;
+    }
+
     // set inputs
     for (int i = 0; i < request.inputs.size(); ++i) {
         const auto& input = request.inputs[i];
@@ -107,6 +112,9 @@
         }
     }
 
+    if (request.outputs.size() > model.getOutputs().size()) {
+        return ErrorStatus::INVALID_ARGUMENT;
+    }
     // set outputs
     for (int i = 0; i < request.outputs.size(); ++i) {
         const auto& output = request.outputs[i];