fix-overflow.patch 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. diff --git a/media/libspeex_resampler/fix-overflow.patch b/media/libspeex_resampler/fix-overflow.patch
  2. new file mode 100644
  3. index 0000000..e69de29
  4. diff --git a/media/libspeex_resampler/src/resample.c b/media/libspeex_resampler/src/resample.c
  5. index a3859e3..d99595a 100644
  6. --- a/media/libspeex_resampler/src/resample.c
  7. +++ b/media/libspeex_resampler/src/resample.c
  8. @@ -98,6 +98,10 @@ static void speex_free (void *ptr) {free(ptr);}
  9. #define NULL 0
  10. #endif
  11. +#ifndef UINT32_MAX
  12. +#define UINT32_MAX 4294967296U
  13. +#endif
  14. +
  15. #include "simd_detect.h"
  16. /* Numer of elements to allocate on the stack */
  17. @@ -603,6 +607,22 @@ static int resampler_basic_zero(SpeexResamplerState *st, spx_uint32_t channel_in
  18. return out_sample;
  19. }
  20. +static int _muldiv_safe(spx_uint32_t value, spx_uint32_t mul, spx_uint32_t div)
  21. +{
  22. + /* TODO: Could be simplified with 64 bits operation. */
  23. + spx_uint32_t major = value / div;
  24. + spx_uint32_t remainder = value % div;
  25. + return remainder <= UINT32_MAX / mul && major <= UINT32_MAX / mul &&
  26. + major * mul <= UINT32_MAX - remainder * mul / div;
  27. +}
  28. +
  29. +static spx_uint32_t _muldiv(spx_uint32_t value, spx_uint32_t mul, spx_uint32_t div)
  30. +{
  31. + spx_uint32_t major = value / div;
  32. + spx_uint32_t remainder = value % div;
  33. + return remainder * mul / div + major * mul;
  34. +}
  35. +
  36. static int update_filter(SpeexResamplerState *st)
  37. {
  38. spx_uint32_t old_length = st->filt_len;
  39. @@ -620,8 +640,9 @@ static int update_filter(SpeexResamplerState *st)
  40. {
  41. /* down-sampling */
  42. st->cutoff = quality_map[st->quality].downsample_bandwidth * st->den_rate / st->num_rate;
  43. - /* FIXME: divide the numerator and denominator by a certain amount if they're too large */
  44. - st->filt_len = st->filt_len*st->num_rate / st->den_rate;
  45. + if (!_muldiv_safe(st->filt_len,st->num_rate,st->den_rate))
  46. + goto fail;
  47. + st->filt_len = _muldiv(st->filt_len,st->num_rate,st->den_rate);
  48. /* Round up to make sure we have a multiple of 8 for SSE */
  49. st->filt_len = ((st->filt_len-1)&(~0x7))+8;
  50. if (2*st->den_rate < st->num_rate)
  51. @@ -1129,7 +1150,9 @@ EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t r
  52. {
  53. for (i=0;i<st->nb_channels;i++)
  54. {
  55. - st->samp_frac_num[i]=st->samp_frac_num[i]*st->den_rate/old_den;
  56. + if (!_muldiv_safe(st->samp_frac_num[i],st->den_rate,old_den))
  57. + return RESAMPLER_ERR_OVERFLOW;
  58. + st->samp_frac_num[i]= _muldiv(st->samp_frac_num[i],st->den_rate,old_den);
  59. /* Safety net */
  60. if (st->samp_frac_num[i] >= st->den_rate)
  61. st->samp_frac_num[i] = st->den_rate-1;
  62. diff --git a/media/libspeex_resampler/src/speex_resampler.h b/media/libspeex_resampler/src/speex_resampler.h
  63. index 70abe52..1286872 100644
  64. --- a/media/libspeex_resampler/src/speex_resampler.h
  65. +++ b/media/libspeex_resampler/src/speex_resampler.h
  66. @@ -106,7 +106,8 @@ enum {
  67. RESAMPLER_ERR_BAD_STATE = 2,
  68. RESAMPLER_ERR_INVALID_ARG = 3,
  69. RESAMPLER_ERR_PTR_OVERLAP = 4,
  70. -
  71. + RESAMPLER_ERR_OVERFLOW = 5,
  72. +
  73. RESAMPLER_ERR_MAX_ERROR
  74. };
  75. diff --git a/media/libspeex_resampler/update.sh b/media/libspeex_resampler/update.sh
  76. index d4a025b..6950bc6 100644
  77. --- a/media/libspeex_resampler/update.sh
  78. +++ b/media/libspeex_resampler/update.sh
  79. @@ -26,3 +26,4 @@ patch -p3 < set-skip-frac.patch
  80. patch -p3 < hugemem.patch
  81. patch -p3 < remove-empty-asm-clobber.patch
  82. patch -p3 < handle-memory-error.patch
  83. +patch -p3 < fix-overflow.patch