[FFmpeg-devel,v1,2/4] avformat/hlsenc: remove the unnecessary null pointer check

Submitted by lance.lmwang@gmail.com on Sept. 16, 2019, 1:03 a.m.

Details

Message ID 1568595818-7943-2-git-send-email-lance.lmwang@gmail.com
State New
Headers show

Commit Message

lance.lmwang@gmail.com Sept. 16, 2019, 1:03 a.m.
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavformat/hlsenc.c | 31 ++-----------------------------
 1 file changed, 2 insertions(+), 29 deletions(-)

Comments

Steven Liu Sept. 16, 2019, 3:20 a.m.
> 在 2019年9月16日,上午9:03,lance.lmwang@gmail.com 写道:
> 
> From: Limin Wang <lance.lmwang@gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
> libavformat/hlsenc.c | 31 ++-----------------------------
> 1 file changed, 2 insertions(+), 29 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 1f2bdfbe4d..b5681a85c7 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -1613,16 +1613,13 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
>             if (c->use_localtime_mkdir) {
>                 const char *dir;
>                 char *fn_copy = av_strdup(oc->url);
> -                if (!fn_copy) {
> -                    return AVERROR(ENOMEM);
> -                }
>                 dir = av_dirname(fn_copy);
>                 if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
>                     av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
> -                    av_free(fn_copy);
> +                    av_freep(&fn_copy);
>                     return AVERROR(errno);
>                 }
> -                av_free(fn_copy);
> +                av_freep(&fn_copy);
>             }
>         } else {
>             char *filename = NULL;
> @@ -1784,11 +1781,6 @@ static int validate_name(int nb_vs, const char *fn)
>     }
> 
>     fn_dup = av_strdup(fn);
> -    if (!fn_dup) {
> -        ret = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -
>     filename = av_basename(fn);
>     subdir_name = av_dirname(fn_dup);
> 
> @@ -1818,11 +1810,6 @@ static int format_name(const char *buf, char **s, int index, const char *varname
>     int ret = 0;
> 
>     orig_buf_dup = av_strdup(buf);
> -    if (!orig_buf_dup) {
> -        ret = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -

will segment fault when remove these five lines;
>     if (!av_stristr(buf, "%v")) {
>         *s = orig_buf_dup;
>         return ret;
> @@ -1846,11 +1833,6 @@ static int format_name(const char *buf, char **s, int index, const char *varname
>     /* if %v is present in the file's directory, create sub-directory */
>     if (av_stristr(dir, "%v") && proto && !strcmp(proto, "file")) {
>         mod_buf_dup = av_strdup(*s);
> -        if (!mod_buf_dup) {
> -            ret = AVERROR(ENOMEM);
> -            goto fail;
> -        }
> -
>         dir = av_dirname(mod_buf_dup);
>         if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
>             ret = AVERROR(errno);
> @@ -2151,11 +2133,6 @@ static int update_master_pl_info(AVFormatContext *s)
>     int ret = 0;
> 
>     fn1 = av_strdup(s->url);
> -    if (!fn1) {
> -        ret = AVERROR(ENOMEM);
> -        goto fail;
> -    }
> -
>     dir = av_dirname(fn1);
> 
>     /**
> @@ -2164,10 +2141,6 @@ static int update_master_pl_info(AVFormatContext *s)
>      */
>     if (dir && av_stristr(av_basename(dir), "%v")) {
>         fn2 = av_strdup(dir);
> -        if (!fn2) {
> -            ret = AVERROR(ENOMEM);
> -            goto fail;
> -        }
>         dir = av_dirname(fn2);
>     }
> 
> -- 
> 2.21.0
> 
> _______________________________________________
> 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”.

BTW,

command line:
./ffmpeg -re -f lavfi -i testsrc2 -g 25 -r:v 25 -f hls -hls_time 2  -strftime 1 -strftime_mkdir 1 -hls_segment_filename "http://127.0.0.1/output_liuqi_%s.ts" http://127.0.0.1/output_liuqi.m3u8
it incorrent result when strdup failed.

before patch it should get no memory error message.

after it will not get error message, but it create file at the root dir on server.

StevenLiu:dash StevenLiu$ sudo cat /usr/local/nginx/html/output_liuqi.m3u8 ;sudo ls /usr/local/nginx/html/output_liuqi*.ts
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:111
#EXTINF:2.000000,
http://127.0.0.1/output_liuqi_1568603743.ts
#EXTINF:2.000000,
http://127.0.0.1/output_liuqi_1568603745.ts
#EXTINF:1.640000,
http://127.0.0.1/output_liuqi_1568603747.ts
#EXTINF:2.000000,
http://127.0.0.1/output_liuqi_1568603748.ts
#EXTINF:2.000000,
http://127.0.0.1/output_liuqi_1568603750.ts

/usr/local/nginx/html/output_liuqi_1568603519.ts        /usr/local/nginx/html/output_liuqi_1568603579.ts        /usr/local/nginx/html/output_liuqi_1568603636.ts        /usr/local/nginx/html/output_liuqi_1568603695.ts
/usr/local/nginx/html/output_liuqi_1568603522.ts        /usr/local/nginx/html/output_liuqi_1568603580.ts        /usr/local/nginx/html/output_liuqi_1568603639.ts        /usr/local/nginx/html/output_liuqi_1568603697.ts
/usr/local/nginx/html/output_liuqi_1568603524.ts        /usr/local/nginx/html/output_liuqi_1568603582.ts        /usr/local/nginx/html/output_liuqi_1568603641.ts        /usr/local/nginx/html/output_liuqi_1568603699.ts
/usr/local/nginx/html/output_liuqi_1568603526.ts        /usr/local/nginx/html/output_liuqi_1568603584.ts        /usr/local/nginx/html/output_liuqi_1568603642.ts        /usr/local/nginx/html/output_liuqi_1568603701.ts
/usr/local/nginx/html/output_liuqi_1568603528.ts        /usr/local/nginx/html/output_liuqi_1568603586.ts        /usr/local/nginx/html/output_liuqi_1568603644.ts        /usr/local/nginx/html/output_liuqi_1568603703.ts
/usr/local/nginx/html/output_liuqi_1568603530.ts        /usr/local/nginx/html/output_liuqi_1568603588.ts        /usr/local/nginx/html/output_liuqi_1568603646.ts        /usr/local/nginx/html/output_liuqi_1568603705.ts
/usr/local/nginx/html/output_liuqi_1568603532.ts        /usr/local/nginx/html/output_liuqi_1568603591.ts        /usr/local/nginx/html/output_liuqi_1568603649.ts        /usr/local/nginx/html/output_liuqi_1568603707.ts
/usr/local/nginx/html/output_liuqi_1568603534.ts        /usr/local/nginx/html/output_liuqi_1568603593.ts        /usr/local/nginx/html/output_liuqi_1568603651.ts        /usr/local/nginx/html/output_liuqi_1568603708.ts
/usr/local/nginx/html/output_liuqi_1568603536.ts        /usr/local/nginx/html/output_liuqi_1568603594.ts        /usr/local/nginx/html/output_liuqi_1568603653.ts        /usr/local/nginx/html/output_liuqi_1568603710.ts
/usr/local/nginx/html/output_liuqi_1568603538.ts        /usr/local/nginx/html/output_liuqi_1568603596.ts        /usr/local/nginx/html/output_liuqi_1568603655.ts        /usr/local/nginx/html/output_liuqi_1568603712.ts
/usr/local/nginx/html/output_liuqi_1568603540.ts        /usr/local/nginx/html/output_liuqi_1568603598.ts        /usr/local/nginx/html/output_liuqi_1568603657.ts        /usr/local/nginx/html/output_liuqi_1568603714.ts
/usr/local/nginx/html/output_liuqi_1568603542.ts        /usr/local/nginx/html/output_liuqi_1568603601.ts        /usr/local/nginx/html/output_liuqi_1568603659.ts        /usr/local/nginx/html/output_liuqi_1568603716.ts
/usr/local/nginx/html/output_liuqi_1568603544.ts        /usr/local/nginx/html/output_liuqi_1568603603.ts        /usr/local/nginx/html/output_liuqi_1568603661.ts        /usr/local/nginx/html/output_liuqi_1568603718.ts
/usr/local/nginx/html/output_liuqi_1568603546.ts        /usr/local/nginx/html/output_liuqi_1568603605.ts        /usr/local/nginx/html/output_liuqi_1568603663.ts        /usr/local/nginx/html/output_liuqi_1568603720.ts
/usr/local/nginx/html/output_liuqi_1568603548.ts        /usr/local/nginx/html/output_liuqi_1568603607.ts        /usr/local/nginx/html/output_liuqi_1568603665.ts        /usr/local/nginx/html/output_liuqi_1568603722.ts
/usr/local/nginx/html/output_liuqi_1568603550.ts        /usr/local/nginx/html/output_liuqi_1568603609.ts        /usr/local/nginx/html/output_liuqi_1568603667.ts        /usr/local/nginx/html/output_liuqi_1568603724.ts
/usr/local/nginx/html/output_liuqi_1568603553.ts        /usr/local/nginx/html/output_liuqi_1568603611.ts        /usr/local/nginx/html/output_liuqi_1568603668.ts        /usr/local/nginx/html/output_liuqi_1568603727.ts
/usr/local/nginx/html/output_liuqi_1568603555.ts        /usr/local/nginx/html/output_liuqi_1568603613.ts        /usr/local/nginx/html/output_liuqi_1568603670.ts        /usr/local/nginx/html/output_liuqi_1568603728.ts
/usr/local/nginx/html/output_liuqi_1568603557.ts        /usr/local/nginx/html/output_liuqi_1568603615.ts        /usr/local/nginx/html/output_liuqi_1568603672.ts        /usr/local/nginx/html/output_liuqi_1568603730.ts
/usr/local/nginx/html/output_liuqi_1568603558.ts        /usr/local/nginx/html/output_liuqi_1568603617.ts        /usr/local/nginx/html/output_liuqi_1568603674.ts        /usr/local/nginx/html/output_liuqi_1568603732.ts
/usr/local/nginx/html/output_liuqi_1568603560.ts        /usr/local/nginx/html/output_liuqi_1568603619.ts        /usr/local/nginx/html/output_liuqi_1568603676.ts        /usr/local/nginx/html/output_liuqi_1568603734.ts
/usr/local/nginx/html/output_liuqi_1568603562.ts        /usr/local/nginx/html/output_liuqi_1568603621.ts        /usr/local/nginx/html/output_liuqi_1568603679.ts        /usr/local/nginx/html/output_liuqi_1568603737.ts
/usr/local/nginx/html/output_liuqi_1568603564.ts        /usr/local/nginx/html/output_liuqi_1568603623.ts        /usr/local/nginx/html/output_liuqi_1568603681.ts        /usr/local/nginx/html/output_liuqi_1568603739.ts
/usr/local/nginx/html/output_liuqi_1568603567.ts        /usr/local/nginx/html/output_liuqi_1568603625.ts        /usr/local/nginx/html/output_liuqi_1568603683.ts        /usr/local/nginx/html/output_liuqi_1568603741.ts
/usr/local/nginx/html/output_liuqi_1568603569.ts        /usr/local/nginx/html/output_liuqi_1568603627.ts        /usr/local/nginx/html/output_liuqi_1568603684.ts        /usr/local/nginx/html/output_liuqi_1568603743.ts
/usr/local/nginx/html/output_liuqi_1568603571.ts        /usr/local/nginx/html/output_liuqi_1568603629.ts        /usr/local/nginx/html/output_liuqi_1568603686.ts        /usr/local/nginx/html/output_liuqi_1568603745.ts
/usr/local/nginx/html/output_liuqi_1568603573.ts        /usr/local/nginx/html/output_liuqi_1568603631.ts        /usr/local/nginx/html/output_liuqi_1568603688.ts        /usr/local/nginx/html/output_liuqi_1568603747.ts
/usr/local/nginx/html/output_liuqi_1568603575.ts        /usr/local/nginx/html/output_liuqi_1568603633.ts        /usr/local/nginx/html/output_liuqi_1568603691.ts        /usr/local/nginx/html/output_liuqi_1568603748.ts
/usr/local/nginx/html/output_liuqi_1568603577.ts        /usr/local/nginx/html/output_liuqi_1568603635.ts        /usr/local/nginx/html/output_liuqi_1568603693.ts        /usr/local/nginx/html/output_liuqi_1568603750.ts
StevenLiu:dash StevenLiu$

Patch hide | download patch | download mbox

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 1f2bdfbe4d..b5681a85c7 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -1613,16 +1613,13 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
             if (c->use_localtime_mkdir) {
                 const char *dir;
                 char *fn_copy = av_strdup(oc->url);
-                if (!fn_copy) {
-                    return AVERROR(ENOMEM);
-                }
                 dir = av_dirname(fn_copy);
                 if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
                     av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
-                    av_free(fn_copy);
+                    av_freep(&fn_copy);
                     return AVERROR(errno);
                 }
-                av_free(fn_copy);
+                av_freep(&fn_copy);
             }
         } else {
             char *filename = NULL;
@@ -1784,11 +1781,6 @@  static int validate_name(int nb_vs, const char *fn)
     }
 
     fn_dup = av_strdup(fn);
-    if (!fn_dup) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
     filename = av_basename(fn);
     subdir_name = av_dirname(fn_dup);
 
@@ -1818,11 +1810,6 @@  static int format_name(const char *buf, char **s, int index, const char *varname
     int ret = 0;
 
     orig_buf_dup = av_strdup(buf);
-    if (!orig_buf_dup) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
     if (!av_stristr(buf, "%v")) {
         *s = orig_buf_dup;
         return ret;
@@ -1846,11 +1833,6 @@  static int format_name(const char *buf, char **s, int index, const char *varname
     /* if %v is present in the file's directory, create sub-directory */
     if (av_stristr(dir, "%v") && proto && !strcmp(proto, "file")) {
         mod_buf_dup = av_strdup(*s);
-        if (!mod_buf_dup) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
-
         dir = av_dirname(mod_buf_dup);
         if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
             ret = AVERROR(errno);
@@ -2151,11 +2133,6 @@  static int update_master_pl_info(AVFormatContext *s)
     int ret = 0;
 
     fn1 = av_strdup(s->url);
-    if (!fn1) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
     dir = av_dirname(fn1);
 
     /**
@@ -2164,10 +2141,6 @@  static int update_master_pl_info(AVFormatContext *s)
      */
     if (dir && av_stristr(av_basename(dir), "%v")) {
         fn2 = av_strdup(dir);
-        if (!fn2) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
         dir = av_dirname(fn2);
     }