Bug 1264199: [speex] P7. Handle memory allocation failures during initialization. r=kinetik draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Tue, 19 Apr 2016 14:39:39 +1000
changeset 356286 4d2bca2c8531b5dcf8a0e4e634f20c0e353078fa
parent 356285 ae063077e30d397739952d9d2c5e61ba640242bb
child 356287 4130d3707ef8f207b63ff78b241fb76a8c7c2838
push id16486
push userbmo:jyavenard@mozilla.com
push dateTue, 26 Apr 2016 02:36:37 +0000
reviewerskinetik
bugs1264199
milestone49.0a1
Bug 1264199: [speex] P7. Handle memory allocation failures during initialization. r=kinetik Fix submitted upstream. MozReview-Commit-ID: JpHoVKu2S7w
media/libspeex_resampler/handle-memory-error.patch
media/libspeex_resampler/src/resample.c
media/libspeex_resampler/update.sh
new file mode 100644
--- /dev/null
+++ b/media/libspeex_resampler/handle-memory-error.patch
@@ -0,0 +1,46 @@
+diff --git a/media/libspeex_resampler/src/resample.c b/media/libspeex_resampler/src/resample.c
+index 83ad119..a3859e3 100644
+--- a/media/libspeex_resampler/src/resample.c
++++ b/media/libspeex_resampler/src/resample.c
+@@ -811,6 +811,12 @@ EXPORT SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
+       return NULL;
+    }
+    st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
++   if (!st)
++   {
++      if (err)
++         *err = RESAMPLER_ERR_ALLOC_FAILED;
++      return NULL;
++   }
+    st->initialised = 0;
+    st->started = 0;
+    st->in_rate = 0;
+@@ -832,9 +838,12 @@ EXPORT SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
+    st->buffer_size = 160;
+    
+    /* Per channel data */
+-   st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t));
+-   st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
+-   st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
++   if (!(st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t))))
++      goto fail;
++   if (!(st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t))))
++      goto fail;
++   if (!(st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t))))
++      goto fail;
+    for (i=0;i<nb_channels;i++)
+    {
+       st->last_sample[i] = 0;
+@@ -857,6 +866,12 @@ EXPORT SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
+       *err = filter_err;
+ 
+    return st;
++
++fail:
++   if (err)
++      *err = RESAMPLER_ERR_ALLOC_FAILED;
++   speex_resampler_destroy(st);
++   return NULL;
+ }
+ 
+ EXPORT void speex_resampler_destroy(SpeexResamplerState *st)
--- a/media/libspeex_resampler/src/resample.c
+++ b/media/libspeex_resampler/src/resample.c
@@ -806,16 +806,22 @@ EXPORT SpeexResamplerState *speex_resamp
 
    if (quality > 10 || quality < 0)
    {
       if (err)
          *err = RESAMPLER_ERR_INVALID_ARG;
       return NULL;
    }
    st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
+   if (!st)
+   {
+      if (err)
+         *err = RESAMPLER_ERR_ALLOC_FAILED;
+      return NULL;
+   }
    st->initialised = 0;
    st->started = 0;
    st->in_rate = 0;
    st->out_rate = 0;
    st->num_rate = 0;
    st->den_rate = 0;
    st->quality = -1;
    st->sinc_table_length = 0;
@@ -827,19 +833,22 @@ EXPORT SpeexResamplerState *speex_resamp
    st->cutoff = 1.f;
    st->nb_channels = nb_channels;
    st->in_stride = 1;
    st->out_stride = 1;
    
    st->buffer_size = 160;
    
    /* Per channel data */
-   st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t));
-   st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
-   st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
+   if (!(st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t))))
+      goto fail;
+   if (!(st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t))))
+      goto fail;
+   if (!(st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t))))
+      goto fail;
    for (i=0;i<nb_channels;i++)
    {
       st->last_sample[i] = 0;
       st->magic_samples[i] = 0;
       st->samp_frac_num[i] = 0;
    }
 
    speex_resampler_set_quality(st, quality);
@@ -852,16 +861,22 @@ EXPORT SpeexResamplerState *speex_resamp
    } else {
       speex_resampler_destroy(st);
       st = NULL;
    }
    if (err)
       *err = filter_err;
 
    return st;
+
+fail:
+   if (err)
+      *err = RESAMPLER_ERR_ALLOC_FAILED;
+   speex_resampler_destroy(st);
+   return NULL;
 }
 
 EXPORT void speex_resampler_destroy(SpeexResamplerState *st)
 {
    speex_free(st->mem);
    speex_free(st->sinc_table);
    speex_free(st->last_sample);
    speex_free(st->magic_samples);
--- a/media/libspeex_resampler/update.sh
+++ b/media/libspeex_resampler/update.sh
@@ -20,8 +20,9 @@ cp $1/AUTHORS .
 cp $1/COPYING .
 
 # apply outstanding local patches
 patch -p3 < outside-speex.patch
 patch -p3 < simd-detect-runtime.patch
 patch -p3 < set-skip-frac.patch
 patch -p3 < hugemem.patch
 patch -p3 < remove-empty-asm-clobber.patch
+patch -p3 < handle-memory-error.patch