shithub: opus

Download patch

ref: d6c3d3ceaeaa2909619866c33b72655cc4f48e66
parent: 913a1742b960b8cb705132d1ebfcf0718bb994e7
author: Jean-Marc Valin <[email protected]>
date: Sun Jan 30 05:23:40 EST 2011

Error handling in _create() functions

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -156,18 +156,26 @@
 
 CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
 {
+   CELTEncoder *st;
    CELTMode *mode = celt_mode_create(48000, 960, NULL);
-   CELTEncoder *st = celt_encoder_init(
-         (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)),
-         sampling_rate, channels, error);
+   st = (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels));
+   if (st!=NULL && celt_encoder_init(st, sampling_rate, channels, error)==NULL)
+   {
+      celt_encoder_destroy(st);
+      st = NULL;
+   }
    return st;
 }
 
 CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error)
 {
-   return celt_encoder_init_custom(
-         (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)),
-         mode, channels, error);
+   CELTEncoder *st = (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels));
+   if (st!=NULL && celt_encoder_init_custom(st, mode, channels, error)==NULL)
+   {
+      celt_encoder_destroy(st);
+      st = NULL;
+   }
+   return st;
 }
 
 CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error)
@@ -192,7 +200,7 @@
       return NULL;
    }
 
-   if (st==NULL)
+   if (st==NULL || mode==NULL)
    {
       if (error)
          *error = CELT_ALLOC_FAIL;
@@ -1754,17 +1762,26 @@
 
 CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
 {
+   CELTDecoder *st;
    const CELTMode *mode = celt_mode_create(48000, 960, NULL);
-   return celt_decoder_init(
-         (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)),
-         sampling_rate, channels, error);
+   st = (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels));
+   if (st!=NULL && celt_decoder_init(st, sampling_rate, channels, error)==NULL)
+   {
+      celt_decoder_destroy(st);
+      st = NULL;
+   }
+   return st;
 }
 
 CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error)
 {
-   return celt_decoder_init_custom(
-         (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)),
-         mode, channels, error);
+   CELTDecoder *st = (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels));
+   if (st!=NULL && celt_decoder_init_custom(st, mode, channels, error)==NULL)
+   {
+      celt_decoder_destroy(st);
+      st = NULL;
+   }
+   return st;
 }
 
 CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error)