diff mbox

[FFmpeg-devel,03/16] avformat/hlsenc: Only allocate when data is known to be needed

Message ID 20191216000418.24707-4-andreas.rheinhardt@gmail.com
State Accepted
Commit ae84305036ceced3813fb295b71d51343ffd9535
Headers show

Commit Message

Andreas Rheinhardt Dec. 16, 2019, 12:04 a.m. UTC
hls_init() would allocate a buffer, although it is only needed in one of
two branches that follow. This commit moves the allocation to the branch
that actually needs the buffer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/hlsenc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

Comments

Liu Steven Dec. 23, 2019, 3:55 a.m. UTC | #1
> 在 2019年12月16日,08:04,Andreas Rheinhardt <andreas.rheinhardt@gmail.com> 写道:
> 
> hls_init() would allocate a buffer, although it is only needed in one of
> two branches that follow. This commit moves the allocation to the branch
> that actually needs the buffer.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
> libavformat/hlsenc.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 965c05c560..87bbfb8086 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -2906,22 +2906,21 @@ static int hls_init(AVFormatContext *s)
>                 ret = AVERROR(ENOMEM);
>                 goto fail;
>             }
> -            vs->vtt_m3u8_name = av_malloc(vtt_basename_size);
> -            if (!vs->vtt_m3u8_name ) {
> -                ret = AVERROR(ENOMEM);
> -                goto fail;
> -            }
>             av_strlcpy(vs->vtt_basename, vs->m3u8_name, vtt_basename_size);
>             p = strrchr(vs->vtt_basename, '.');
>             if (p)
>                 *p = '\0';
> 
>             if ( hls->subtitle_filename ) {
> -                av_freep(&vs->vtt_m3u8_name);
>                 ret = format_name(hls->subtitle_filename, &vs->vtt_m3u8_name, i, vs->varname);
>                 if (ret < 0)
>                     goto fail;
>             } else {
> +                vs->vtt_m3u8_name = av_malloc(vtt_basename_size);
> +                if (!vs->vtt_m3u8_name) {
> +                    ret = AVERROR(ENOMEM);
> +                    goto fail;
> +                }
>                 strcpy(vs->vtt_m3u8_name, vs->vtt_basename);
>                 av_strlcat(vs->vtt_m3u8_name, "_vtt.m3u8", vtt_basename_size);
>             }
> -- 
> 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 965c05c560..87bbfb8086 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2906,22 +2906,21 @@  static int hls_init(AVFormatContext *s)
                 ret = AVERROR(ENOMEM);
                 goto fail;
             }
-            vs->vtt_m3u8_name = av_malloc(vtt_basename_size);
-            if (!vs->vtt_m3u8_name ) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
             av_strlcpy(vs->vtt_basename, vs->m3u8_name, vtt_basename_size);
             p = strrchr(vs->vtt_basename, '.');
             if (p)
                 *p = '\0';
 
             if ( hls->subtitle_filename ) {
-                av_freep(&vs->vtt_m3u8_name);
                 ret = format_name(hls->subtitle_filename, &vs->vtt_m3u8_name, i, vs->varname);
                 if (ret < 0)
                     goto fail;
             } else {
+                vs->vtt_m3u8_name = av_malloc(vtt_basename_size);
+                if (!vs->vtt_m3u8_name) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
                 strcpy(vs->vtt_m3u8_name, vs->vtt_basename);
                 av_strlcat(vs->vtt_m3u8_name, "_vtt.m3u8", vtt_basename_size);
             }