common array: + exception handling.

properly handle if the realloc() is failed.

@Isssue: https://github.com/Samsung/thorvg/issues/995
This commit is contained in:
Hermet Park 2021-11-05 20:53:39 +09:00 committed by Hermet Park
parent 8608238343
commit 2aa551e222

View file

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