common array: + exception handling.
authorHermet Park <chuneon.park@samsung.com>
Fri, 5 Nov 2021 11:53:39 +0000 (20:53 +0900)
committerJunsuChoi <jsuya.choi@samsung.com>
Mon, 8 Nov 2021 09:01:54 +0000 (18:01 +0900)
properly handle if the realloc() is failed.

@Isssue: https://github.com/Samsung/thorvg/issues/995

src/lib/tvgArray.h

index 7283a83..d04e68e 100644 (file)
@@ -38,7 +38,12 @@ struct Array
     {
         if (count + 1 > reserved) {
             reserved = (count + 1) * 2;
+            auto p  = data;
             data = static_cast<T*>(realloc(data, sizeof(T) * reserved));
+            if (!data) {
+                data = p;
+                return;
+            }
         }
         data[count++] = element;
     }
@@ -47,8 +52,12 @@ struct Array
     {
         if (size > reserved) {
             reserved = size;
+            auto p = data;
             data = static_cast<T*>(realloc(data, sizeof(T) * reserved));
-            if (!data) return false;
+            if (!data) {
+                data = p;
+                return false;
+            }
         }
         return true;
     }