[tensor] Add checks for external tensors
authorParichay Kapoor <pk.kapoor@samsung.com>
Tue, 19 Oct 2021 02:40:52 +0000 (11:40 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 19 Oct 2021 11:35:38 +0000 (20:35 +0900)
This patch adds checks while setting the external tensors:
- ensure that the external tensor size is more than the required memory
- if the tensor size is 0, its data must be null

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
nntrainer/tensor/tensor_pool.h

index 84b59b5283d4d1e2ecd4f7c828911f5deadbbda2..80f69f6b62ef632bf5412cec505c8ab87807d0e0 100644 (file)
@@ -204,6 +204,13 @@ public:
       throw std::invalid_argument(
         "Cannot set external tensor for non-zero lifespan");
 
+    if (t.size() == 0 && t.getData())
+      throw std::invalid_argument(
+        "Error: setting invalid external tensor size 0 for " + name);
+    if (t.size() != 0 && t.size() < spec.tensor->size())
+      throw std::invalid_argument(
+        "Error: setting external tensor of smaller size for " + name);
+
     spec.tensor->setData(t.getData());
   }