diff mbox

[FFmpeg-devel,09/11] avutil/dict: Fix memleak when using AV_DICT_APPEND

Message ID 20191111011259.27313-1-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Nov. 11, 2019, 1:12 a.m. UTC
If a key already exists in an AVDictionary and the AV_DICT_APPEND flag
is set, the old entry is at first discarded from the dictionary, but
a pointer to the value is kept. Lateron enough memory to store the
appended string is allocated; should this allocation fail, the old string
is not freed and hence leaks. This commit changes this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavutil/dict.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/libavutil/dict.c b/libavutil/dict.c
index 0ea71386e5..190ef196be 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -115,8 +115,10 @@  int av_dict_set(AVDictionary **pm, const char *key, const char *value,
         if (oldval && flags & AV_DICT_APPEND) {
             size_t len = strlen(oldval) + strlen(copy_value) + 1;
             char *newval = av_mallocz(len);
-            if (!newval)
+            if (!newval) {
+                av_free(oldval);
                 goto err_out;
+            }
             av_strlcat(newval, oldval, len);
             av_freep(&oldval);
             av_strlcat(newval, copy_value, len);