shithub: libopusenc

Download patch

ref: 3e73d2ee6c3dedcb4b361cae6db327973e771ae0
parent: 902d9a07ff5ec952d80a6ea98fedaf557503fc7f
author: Mark Harris <mark.hsj@gmail.com>
date: Sun Jan 14 16:12:54 EST 2018

Fix allocation failure cleanup

Signed-off-by: Jean-Marc Valin <jmvalin@jmvalin.ca>

--- a/src/opus_header.c
+++ b/src/opus_header.c
@@ -191,11 +191,14 @@
   int user_comment_list_length=0;
   int len=8+4+vendor_length+4;
   char *p=(char*)malloc(len);
-  if (p == NULL) return;
-  memcpy(p, "OpusTags", 8);
-  writeint(p, 8, vendor_length);
-  memcpy(p+12, vendor_string, vendor_length);
-  writeint(p, 12+vendor_length, user_comment_list_length);
+  if (p == NULL) {
+    len=0;
+  } else {
+    memcpy(p, "OpusTags", 8);
+    writeint(p, 8, vendor_length);
+    memcpy(p+12, vendor_string, vendor_length);
+    writeint(p, 12+vendor_length, user_comment_list_length);
+  }
   *length=len;
   *comments=p;
 }
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -231,8 +231,13 @@
   OggOpusEnc *enc;
   struct StdioObject *obj;
   obj = malloc(sizeof(*obj));
+  if (obj == NULL) {
+    if (error) *error = OPE_ALLOC_FAIL;
+    return NULL;
+  }
   enc = ope_encoder_create_callbacks(&stdio_callbacks, obj, comments, rate, channels, family, error);
   if (enc == NULL || (error && *error)) {
+    free(obj);
     return NULL;
   }
   obj->file = _ope_fopen(path, "wb");
@@ -291,9 +296,9 @@
   }
 
   if ( (enc = malloc(sizeof(*enc))) == NULL) goto fail;
-  enc->streams = NULL;
+  enc->buffer = NULL;
+  enc->lpc_buffer = NULL;
   if ( (enc->streams = stream_create(comments)) == NULL) goto fail;
-  enc->streams->next = NULL;
   enc->last_stream = enc->streams;
   enc->oggp = NULL;
   enc->unrecoverable = 0;
@@ -334,8 +339,6 @@
     /* Allocate an extra LPC_PADDING samples so we can do the padding in-place. */
     if ( (enc->lpc_buffer = malloc(sizeof(*enc->lpc_buffer)*(LPC_INPUT+LPC_PADDING)*channels)) == NULL) goto fail;
     memset(enc->lpc_buffer, 0, sizeof(*enc->lpc_buffer)*LPC_INPUT*channels);
-  } else {
-    enc->lpc_buffer = NULL;
   }
   enc->buffer_start = enc->buffer_end = 0;
   enc->st = st;
@@ -348,10 +351,10 @@
   return enc;
 fail:
   if (enc) {
-    free(enc);
     if (enc->buffer) free(enc->buffer);
     if (enc->streams) stream_destroy(enc->streams);
     if (enc->lpc_buffer) free(enc->lpc_buffer);
+    free(enc);
   }
   if (st) {
     opus_multistream_encoder_destroy(st);