From 50764b3db235dab5719f4408038b1718c054a843 Mon Sep 17 00:00:00 2001
From: NAUD Maxence <maxence.naud@cea.fr>
Date: Tue, 17 Oct 2023 12:21:11 +0000
Subject: [PATCH] [Fix] memory leak due to dim variable overflow

---
 include/aidge/data/Tensor.hpp | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/include/aidge/data/Tensor.hpp b/include/aidge/data/Tensor.hpp
index 7422a52eb..a74667859 100644
--- a/include/aidge/data/Tensor.hpp
+++ b/include/aidge/data/Tensor.hpp
@@ -473,17 +473,18 @@ class Tensor : public Data,
 
 
 
-    std::string toString() {
+   std::string toString() {
         if (dims().empty()) { return "{}"; }
         std::string res;
         std::size_t dim = 0;
-        std::size_t *dimVals = new std::size_t[nbDims()];
-        for (std::size_t i = 0; i < nbDims(); ++i) {
-            dimVals[i] = 0;
-        }
         std::size_t counter = 0;
-        res += "{\n";
-        if (nbDims()>=2){
+        if (nbDims()>=2) {
+            std::size_t *dimVals = new std::size_t[nbDims()];
+            for (std::size_t i = 0; i < nbDims(); ++i) {
+                dimVals[i] = 0;
+            }
+            // std::vector<std::size_t> dimVals = std::vector<std::size_t>(nbDims(), 0);
+            res += "{\n";
             while (counter < mSize) {
                 std::string spaceString = std::string((dim+1)<<1,' ');
                 if (dim < nbDims()-2) {
@@ -532,31 +533,35 @@ class Tensor : public Data,
                         }
                         res += "\n";
                     }
+                    if (dim == 0) {
+                        break;
+                    }
                     dimVals[dim--] = 0;
                     dimVals[dim]++;
                 }
             }
-            for(int i = static_cast<int>(dim); i>=0; --i) {
+            delete[] dimVals;
+
+            for(int i = static_cast<int>(dim); i > 0; --i) {
                 res += std::string((dim+1)<<1,' ') + "}\n";
             }
-        }else{
+        } else {
+            res += "{";
             for (DimSize_t j = 0; j < dims()[0]; ++j) {
                 switch (mDataType)
                 {
                 case DataType::Int32:
-                    res += " " + std::to_string(static_cast<int *>(mImpl->rawPtr())[j]) + ((j < dims()[0]-1) ? "," : "\n");
+                    res += " " + std::to_string(static_cast<int *>(mImpl->rawPtr())[j]) + ((j < dims()[0]-1) ? "," : "");
                     break;
                 case DataType::Float64:
-                    res += " " + std::to_string(static_cast<double *>(mImpl->rawPtr())[j]) + ((j < dims()[0]-1) ? "," : "\n");
+                    res += " " + std::to_string(static_cast<double *>(mImpl->rawPtr())[j]) + ((j < dims()[0]-1) ? "," : "");
                     break;
                 default:
-                    res += " " + std::to_string(static_cast<float *>(mImpl->rawPtr())[j]) + ((j < dims()[0]-1) ? "," : "\n");
+                    res += " " + std::to_string(static_cast<float *>(mImpl->rawPtr())[j]) + ((j < dims()[0]-1) ? "," : "");
                     break;
                 }
             }
         }
-
-
         res += "}";
         return res;
     }
-- 
GitLab