[FFmpeg-devel] [PATCH] avutil/dict: fix memleak in av_dict_set()
Michael Niedermayer
michael at niedermayer.cc
Tue Apr 8 03:40:27 EEST 2025
Hi James
On Mon, Apr 07, 2025 at 07:45:23PM -0300, James Almer wrote:
> Regression since 19e9a203b7b8e613840b055cdf68303a4fb84581.
>
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
> libavutil/dict.c | 19 ++++++++++---------
> 1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/libavutil/dict.c b/libavutil/dict.c
> index f7dcd07eeb..fafb454fd3 100644
> --- a/libavutil/dict.c
> +++ b/libavutil/dict.c
> @@ -99,27 +99,28 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value,
> err = AVERROR(EINVAL);
> goto err_out;
> }
> + if (flags & AV_DICT_DONT_STRDUP_KEY)
> + copy_key = (void *)key;
> + else
> + copy_key = av_strdup(key);
> + if (!copy_key || (value && !copy_value))
> + goto enomem;
> +
> if (!(flags & AV_DICT_MULTIKEY)) {
> tag = av_dict_get(m, key, NULL, flags);
> } else if (flags & AV_DICT_DEDUP) {
> while ((tag = av_dict_get(m, key, tag, flags))) {
> if ((!value && !tag->value) ||
> (value && tag->value && !strcmp(value, tag->value))) {
> - if (flags & AV_DICT_DONT_STRDUP_KEY)
> - av_free((void*)key);
> - if (flags & AV_DICT_DONT_STRDUP_VAL)
> - av_free((void*)value);
> + av_free(copy_key);
> + av_free(copy_value);
> return 0;
> }
> }
> }
> - if (flags & AV_DICT_DONT_STRDUP_KEY)
> - copy_key = (void *)key;
> - else
> - copy_key = av_strdup(key);
Should be ok
Maybe slightly better if the strdup isnt moved up as theres
a case then where we strdup and free it.
thx
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Those who are best at talking, realize last or never when they are wrong.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20250408/d1c9e0f1/attachment.sig>
More information about the ffmpeg-devel
mailing list