matroskaenc: Make sure the seekhead struct is freed even on seek failure

Message ID 1320693620-86021-1-git-send-email-martin@martin.st
State Committed
Headers show

Commit Message

Martin Storsjö Nov. 7, 2011, 7:20 p.m.
The caller expects the seekhead struct to be freed when calling
matroska_write_seekhead. Currently, the structure is leaked if the
seek fails.
---
 libavformat/matroskaenc.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)

Comments

Justin Ruggles Nov. 7, 2011, 7:24 p.m. | #1
On 11/07/2011 02:20 PM, Martin Storsjö wrote:

> The caller expects the seekhead struct to be freed when calling
> matroska_write_seekhead. Currently, the structure is leaked if the
> seek fails.
> ---
>  libavformat/matroskaenc.c |   10 +++++++---
>  1 files changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
> index 5edd2be..0667947 100644
> --- a/libavformat/matroskaenc.c
> +++ b/libavformat/matroskaenc.c
> @@ -316,11 +316,14 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
>      int i;
>  
>      currentpos = avio_tell(pb);
>  
> -    if (seekhead->reserved_size > 0)
> -        if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0)
> -            return -1;
> +    if (seekhead->reserved_size > 0) {
> +        if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
> +            currentpos = -1;
> +            goto fail;
> +        }
> +    }
>  
>      metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
>      for (i = 0; i < seekhead->num_entries; i++) {
>          mkv_seekhead_entry *entry = &seekhead->entries[i];
> @@ -342,8 +345,9 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
>          avio_seek(pb, currentpos, SEEK_SET);
>  
>          currentpos = seekhead->filepos;
>      }
> +fail:
>      av_free(seekhead->entries);
>      av_free(seekhead);
>  
>      return currentpos;


patch looks ok.

-Justin

Patch

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 5edd2be..0667947 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -316,11 +316,14 @@  static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
     int i;
 
     currentpos = avio_tell(pb);
 
-    if (seekhead->reserved_size > 0)
-        if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0)
-            return -1;
+    if (seekhead->reserved_size > 0) {
+        if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
+            currentpos = -1;
+            goto fail;
+        }
+    }
 
     metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
     for (i = 0; i < seekhead->num_entries; i++) {
         mkv_seekhead_entry *entry = &seekhead->entries[i];
@@ -342,8 +345,9 @@  static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
         avio_seek(pb, currentpos, SEEK_SET);
 
         currentpos = seekhead->filepos;
     }
+fail:
     av_free(seekhead->entries);
     av_free(seekhead);
 
     return currentpos;