From d50eea2a2c6b6d358d8684d7f27e6c3623e62dec Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Wed, 6 Mar 2024 22:05:51 +0900 Subject: [PATCH] lottie: fix potential memory leaks. memory could be leaked during the slot property overriding. --- src/loaders/lottie/tvgLottieProperty.h | 62 ++++++++++++++++++++------ 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/src/loaders/lottie/tvgLottieProperty.h b/src/loaders/lottie/tvgLottieProperty.h index 7558e894..5b969bfa 100644 --- a/src/loaders/lottie/tvgLottieProperty.h +++ b/src/loaders/lottie/tvgLottieProperty.h @@ -253,9 +253,10 @@ struct LottieGenericProperty : LottieProperty { //shallow copy, used for slot overriding delete(frames); - if (other.frames) frames = other.frames; - else value = other.value; - const_cast(other).frames = nullptr; + if (other.frames) { + frames = other.frames; + const_cast(other).frames = nullptr; + } else value = other.value; return *this; } @@ -363,13 +364,24 @@ struct LottieColorStop : LottieProperty ~LottieColorStop() { - free(value.data); + release(); + } + + void release() + { + if (value.data) { + free(value.data); + value.data = nullptr; + } + if (!frames) return; + for (auto p = frames->begin(); p < frames->end(); ++p) { free((*p).value.data); } free(frames->data); free(frames); + frames = nullptr; } LottieScalarFrame& newFrame() @@ -442,11 +454,15 @@ struct LottieColorStop : LottieProperty LottieColorStop& operator=(const LottieColorStop& other) { //shallow copy, used for slot overriding - delete(frames); - if (other.frames) frames = other.frames; - else value = other.value; + release(); + if (other.frames) { + frames = other.frames; + const_cast(other).frames = nullptr; + } else { + value = other.value; + const_cast(other).value.data = nullptr; + } count = other.count; - const_cast(other).frames = nullptr; return *this; } @@ -523,15 +539,28 @@ struct LottieTextDoc : LottieProperty ~LottieTextDoc() { - free(value.text); - free(value.name); + release(); + } + + void release() + { + if (value.text) { + free(value.text); + value.text = nullptr; + } + if (value.name) { + free(value.name); + value.name = nullptr; + } if (!frames) return; + for (auto p = frames->begin(); p < frames->end(); ++p) { free((*p).value.text); free((*p).value.name); } delete(frames); + frames = nullptr; } LottieScalarFrame& newFrame() @@ -564,10 +593,15 @@ struct LottieTextDoc : LottieProperty LottieTextDoc& operator=(const LottieTextDoc& other) { //shallow copy, used for slot overriding - delete(frames); - if (other.frames) frames = other.frames; - else value = other.value; - const_cast(other).frames = nullptr; + release(); + if (other.frames) { + frames = other.frames; + const_cast(other).frames = nullptr; + } else { + value = other.value; + const_cast(other).value.text = nullptr; + const_cast(other).value.name = nullptr; + } return *this; }