[FFmpeg-devel] [PATCH 3/4] dict.c: Free non-strduped av_dict_set arguments on error.
Reimar Döffinger
Reimar.Doeffinger at gmx.de
Wed Jul 30 20:38:07 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 cb7be9c..2cd67e8 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -91,7 +91,7 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value,
AVDictionaryEntry *tmp = av_realloc(m->elems,
(m->count + 1) * sizeof(*m->elems));
if (!tmp)
- return AVERROR(ENOMEM);
+ goto err_out;
m->elems = tmp;
}
if (value) {
@@ -105,7 +105,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);
@@ -120,6 +120,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