diff mbox

[FFmpeg-devel,11/16] avformat/hlsenc: Fix memleaks with repeating parameters

Message ID 20191216000418.24707-12-andreas.rheinhardt@gmail.com
State Accepted
Commit 5ba3a8958c76d5490bcc9ba4441f03bedd9aebc6
Headers show

Commit Message

Andreas Rheinhardt Dec. 16, 2019, 12:04 a.m. UTC
When a parameter like e.g. language is contained more than once in the
part of var_stream_map pertaining to a single VariantStream, the later
one just overwrites the pointer to the earlier one, leading to a
memleak. This commit changes this by handling the situation gracefully:
The earlier string is silently freed first, so that the last one wins.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
Picky solutions are of course possible, too.

 libavformat/hlsenc.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Steven Liu Dec. 16, 2019, 2:58 a.m. UTC | #1
> 在 2019年12月16日,08:04,Andreas Rheinhardt <andreas.rheinhardt@gmail.com> 写道:
> 
> When a parameter like e.g. language is contained more than once in the
> part of var_stream_map pertaining to a single VariantStream, the later
> one just overwrites the pointer to the earlier one, leading to a
> memleak. This commit changes this by handling the situation gracefully:
> The earlier string is silently freed first, so that the last one wins.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
> Picky solutions are of course possible, too.
> 
> libavformat/hlsenc.c | 7 +++++++
> 1 file changed, 7 insertions(+)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 669641885e..d5682caaf7 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -1941,6 +1941,7 @@ static int parse_variant_stream_mapstring(AVFormatContext *s)
>         while (keyval = av_strtok(varstr, ",", &saveptr2)) {
>             varstr = NULL;
>             if (av_strstart(keyval, "language:", &val)) {
> +                av_free(vs->language);
>                 vs->language = av_strdup(val);
>                 if (!vs->language)
>                     return AVERROR(ENOMEM);
> @@ -1951,16 +1952,19 @@ static int parse_variant_stream_mapstring(AVFormatContext *s)
>                 hls->has_default_key = 1;
>                 continue;
>             } else if (av_strstart(keyval, "name:", &val)) {
> +                av_free(vs->varname);
>                 vs->varname = av_strdup(val);
>                 if (!vs->varname)
>                     return AVERROR(ENOMEM);
>                 continue;
>             } else if (av_strstart(keyval, "agroup:", &val)) {
> +                av_free(vs->agroup);
>                 vs->agroup = av_strdup(val);
>                 if (!vs->agroup)
>                     return AVERROR(ENOMEM);
>                 continue;
>             } else if (av_strstart(keyval, "ccgroup:", &val)) {
> +                av_free(vs->ccgroup);
>                 vs->ccgroup = av_strdup(val);
>                 if (!vs->ccgroup)
>                     return AVERROR(ENOMEM);
> @@ -2052,14 +2056,17 @@ static int parse_cc_stream_mapstring(AVFormatContext *s)
>             ccstr = NULL;
> 
>             if (av_strstart(keyval, "ccgroup:", &val)) {
> +                av_free(ccs->ccgroup);
>                 ccs->ccgroup = av_strdup(val);
>                 if (!ccs->ccgroup)
>                     return AVERROR(ENOMEM);
>             } else if (av_strstart(keyval, "instreamid:", &val)) {
> +                av_free(ccs->instreamid);
>                 ccs->instreamid = av_strdup(val);
>                 if (!ccs->instreamid)
>                     return AVERROR(ENOMEM);
>             } else if (av_strstart(keyval, "language:", &val)) {
> +                av_free(ccs->language);
>                 ccs->language = av_strdup(val);
>                 if (!ccs->language)
>                     return AVERROR(ENOMEM);
> -- 
> 2.20.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

LGTM

Thanks
Steven
diff mbox

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 669641885e..d5682caaf7 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -1941,6 +1941,7 @@  static int parse_variant_stream_mapstring(AVFormatContext *s)
         while (keyval = av_strtok(varstr, ",", &saveptr2)) {
             varstr = NULL;
             if (av_strstart(keyval, "language:", &val)) {
+                av_free(vs->language);
                 vs->language = av_strdup(val);
                 if (!vs->language)
                     return AVERROR(ENOMEM);
@@ -1951,16 +1952,19 @@  static int parse_variant_stream_mapstring(AVFormatContext *s)
                 hls->has_default_key = 1;
                 continue;
             } else if (av_strstart(keyval, "name:", &val)) {
+                av_free(vs->varname);
                 vs->varname = av_strdup(val);
                 if (!vs->varname)
                     return AVERROR(ENOMEM);
                 continue;
             } else if (av_strstart(keyval, "agroup:", &val)) {
+                av_free(vs->agroup);
                 vs->agroup = av_strdup(val);
                 if (!vs->agroup)
                     return AVERROR(ENOMEM);
                 continue;
             } else if (av_strstart(keyval, "ccgroup:", &val)) {
+                av_free(vs->ccgroup);
                 vs->ccgroup = av_strdup(val);
                 if (!vs->ccgroup)
                     return AVERROR(ENOMEM);
@@ -2052,14 +2056,17 @@  static int parse_cc_stream_mapstring(AVFormatContext *s)
             ccstr = NULL;
 
             if (av_strstart(keyval, "ccgroup:", &val)) {
+                av_free(ccs->ccgroup);
                 ccs->ccgroup = av_strdup(val);
                 if (!ccs->ccgroup)
                     return AVERROR(ENOMEM);
             } else if (av_strstart(keyval, "instreamid:", &val)) {
+                av_free(ccs->instreamid);
                 ccs->instreamid = av_strdup(val);
                 if (!ccs->instreamid)
                     return AVERROR(ENOMEM);
             } else if (av_strstart(keyval, "language:", &val)) {
+                av_free(ccs->language);
                 ccs->language = av_strdup(val);
                 if (!ccs->language)
                     return AVERROR(ENOMEM);