sw_engine mempool: fixed to address a potential memory corruption issue. 51/292351/1
authorHermet Park <hermetpark@gmail.com>
Mon, 24 Apr 2023 08:18:32 +0000 (17:18 +0900)
committerPatryk Kaczmarek <patryk.k@partner.samsung.com>
Thu, 4 May 2023 09:24:56 +0000 (11:24 +0200)
The previous memory pool was being shared by both the main and first threads,
which could lead to corruption if any threading changes occurred.

@Issue: https://github.com/thorvg/thorvg/issues/1370

Change-Id: I1bfb6948eb3bfe3d222f5955f877a7c9ad4f4f78

src/lib/sw_engine/tvgSwMemPool.cpp
src/lib/tvgTaskScheduler.cpp

index a87b3fd..05ff9dd 100644 (file)
@@ -60,16 +60,16 @@ void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx)
 
 SwMpool* mpoolInit(unsigned threads)
 {
-    if (threads == 0) threads = 1;
+    auto allocSize = threads + 1;
 
     auto mpool = static_cast<SwMpool*>(calloc(sizeof(SwMpool), 1));
-    mpool->outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * threads));
+    mpool->outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * allocSize));
     if (!mpool->outline) goto err;
 
-    mpool->strokeOutline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * threads));
+    mpool->strokeOutline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * allocSize));
     if (!mpool->strokeOutline) goto err;
 
-    mpool->allocSize = threads;
+    mpool->allocSize = allocSize;
 
     return mpool;
 
index 819c8c4..4aef6b3 100644 (file)
@@ -136,7 +136,7 @@ public:
             }
 
             if (!success && !taskQueues[i].pop(&task)) break;
-            (*task)(i);
+            (*task)(i + 1);
         }
     }