[FFmpeg-devel] [PATCH 1/3] dict.c: Free non-strduped av_dict_set arguments on error.

Reimar Döffinger Reimar.Doeffinger at gmx.de
Tue Jul 29 21:26:48 CEST 2014


Unfortunately this was not explicitly documented and thus
might be very risky.
But basically all uses I saw in FFmpeg had a memleak in these
cases.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>
---
 libavutil/dict.c | 9 +++++++--
 libavutil/dict.h | 2 ++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavutil/dict.c b/libavutil/dict.c
index 0875b4d..66a0773 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -93,7 +93,7 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value,
         if (tmp)
             m->elems = tmp;
         else
-            return AVERROR(ENOMEM);
+            goto err_out;
     }
     if (value) {
         if (flags & AV_DICT_DONT_STRDUP_KEY)
@@ -106,7 +106,7 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value,
             int len = strlen(oldval) + strlen(value) + 1;
             char *newval = av_mallocz(len);
             if (!newval)
-                return AVERROR(ENOMEM);
+                goto err_out;
             av_strlcat(newval, oldval, len);
             av_freep(&oldval);
             av_strlcat(newval, value, len);
@@ -121,6 +121,11 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value,
     }
 
     return 0;
+
+err_out:
+    if (flags & AV_DICT_DONT_STRDUP_KEY) av_free(key);
+    if (flags & AV_DICT_DONT_STRDUP_VAL) av_free(value);
+    return AVERROR(ENOMEM);
 }
 
 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
diff --git a/libavutil/dict.h b/libavutil/dict.h
index 06f1621..5e319fe 100644
--- a/libavutil/dict.h
+++ b/libavutil/dict.h
@@ -115,6 +115,8 @@ int av_dict_count(const AVDictionary *m);
 /**
  * Set the given entry in *pm, overwriting an existing entry.
  *
+ * Note: On error non-av_strduped arguments will be freed.
+ *
  * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL
  * a dictionary struct is allocated and put in *pm.
  * @param key entry key to add to *pm (will be av_strduped depending on flags)
-- 
2.0.1



More information about the ffmpeg-devel mailing list