Fix the validation for ANeuralNetworksExecution_setInput & setOutput

  - For optional inputs, buffer must be nullptr and length must be 0.
    Passing a nullptr for buffer and a non-zero length is invalid.

Bug: 74829782
Test: mm
Test: ValidationTestExecution#SetInput pass
Merged-In: Icdbc4fd98f0d9bfb35a58bff7f6902dc23123548
Change-Id: Icdbc4fd98f0d9bfb35a58bff7f6902dc23123548
(cherry picked from commit 4ee942c5df0d579baf83e9fb26596ae04896e20c)
diff --git a/runtime/NeuralNetworks.cpp b/runtime/NeuralNetworks.cpp
index 6ee8659..9de91bd 100644
--- a/runtime/NeuralNetworks.cpp
+++ b/runtime/NeuralNetworks.cpp
@@ -428,8 +428,7 @@
 int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution* execution, int32_t index,
                                       const ANeuralNetworksOperandType* type, const void* buffer,
                                       size_t length) {
-    // TODO: For a non-optional input, also verify that buffer is not null.
-    if (!execution) {
+    if (!execution || (!buffer && length != 0)) {
         LOG(ERROR) << "ANeuralNetworksExecution_setInput passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }
@@ -454,7 +453,7 @@
 int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution* execution, int32_t index,
                                        const ANeuralNetworksOperandType* type, void* buffer,
                                        size_t length) {
-    if (!execution || !buffer) {
+    if (!execution || (!buffer && length != 0)) {
         LOG(ERROR) << "ANeuralNetworksExecution_setOutput passed a nullptr";
         return ANEURALNETWORKS_UNEXPECTED_NULL;
     }