diff mbox

[FFmpeg-devel,2/3] avcodec/bsf: fix resource leak in av_bsf_list_parse_str

Message ID 20170110114458.7573-1-lq@chinaffmpeg.org
State Superseded
Headers show

Commit Message

Liu Steven Jan. 10, 2017, 11:44 a.m. UTC
cid: 1396268
when av_strdup(str) error, the lst need release

Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavcodec/bsf.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

Comments

James Almer Jan. 10, 2017, 4:25 p.m. UTC | #1
On 1/10/2017 8:44 AM, Steven Liu wrote:
> cid: 1396268
> when av_strdup(str) error, the lst need release
> 
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>  libavcodec/bsf.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c
> index ac2024b..c9b1df2 100644
> --- a/libavcodec/bsf.c
> +++ b/libavcodec/bsf.c
> @@ -514,8 +514,10 @@ int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
>      if (!lst)
>          return AVERROR(ENOMEM);
>  
> -    if (!(dup = buf = av_strdup(str)))
> -        return AVERROR(ENOMEM);
> +    if (!(dup = buf = av_strdup(str))) {
> +        ret = AVERROR(ENOMEM);
> +        goto free_lst;
> +    }
>  
>      while (1) {
>          bsf_str = av_strtok(buf, ",", &saveptr);
> @@ -524,16 +526,17 @@ int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
>  
>          ret = bsf_parse_single(bsf_str, lst);
>          if (ret < 0)
> -            goto end;
> +            goto free_all;
>  
>          buf = NULL;
>      }
>  
>      ret = av_bsf_list_finalize(&lst, bsf_lst);
> -end:
> +free_all:
> +    av_free(dup);
> +free_lst:
>      if (ret < 0)
>          av_bsf_list_free(&lst);
> -    av_free(dup);

No need for this added complexity. Just set ret to ENOMEM and goto end if
av_strdup() fails, then free dup with av_freep(&dup) here.

>      return ret;
>  }
>  
>
diff mbox

Patch

diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c
index ac2024b..c9b1df2 100644
--- a/libavcodec/bsf.c
+++ b/libavcodec/bsf.c
@@ -514,8 +514,10 @@  int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
     if (!lst)
         return AVERROR(ENOMEM);
 
-    if (!(dup = buf = av_strdup(str)))
-        return AVERROR(ENOMEM);
+    if (!(dup = buf = av_strdup(str))) {
+        ret = AVERROR(ENOMEM);
+        goto free_lst;
+    }
 
     while (1) {
         bsf_str = av_strtok(buf, ",", &saveptr);
@@ -524,16 +526,17 @@  int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
 
         ret = bsf_parse_single(bsf_str, lst);
         if (ret < 0)
-            goto end;
+            goto free_all;
 
         buf = NULL;
     }
 
     ret = av_bsf_list_finalize(&lst, bsf_lst);
-end:
+free_all:
+    av_free(dup);
+free_lst:
     if (ret < 0)
         av_bsf_list_free(&lst);
-    av_free(dup);
     return ret;
 }