Skip to content

Commit

Permalink
Add assertions to detect OOB access in SINC
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire authored and evpobr committed Oct 19, 2020
1 parent e209349 commit 54186d5
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/src_sinc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -299,9 +300,10 @@ calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t star
{ if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index < filter->b_len) ;
assert (data_index < filter->b_end) ;
left += icoeff * filter->buffer [data_index] ;
} ;

Expand All @@ -320,9 +322,10 @@ calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t star
do
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index < filter->b_len) ;
assert (data_index < filter->b_end) ;
right += icoeff * filter->buffer [data_index] ;

filter_index -= increment ;
Expand Down Expand Up @@ -446,9 +449,10 @@ calc_output_stereo (SINC_FILTER *filter, increment_t increment, increment_t star
{ if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + 1 < filter->b_len) ;
assert (data_index + 1 < filter->b_end) ;
left [0] += icoeff * filter->buffer [data_index] ;
left [1] += icoeff * filter->buffer [data_index + 1] ;
} ;
Expand All @@ -468,9 +472,10 @@ calc_output_stereo (SINC_FILTER *filter, increment_t increment, increment_t star
do
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + 1 < filter->b_len) ;
assert (data_index + 1 < filter->b_end) ;
right [0] += icoeff * filter->buffer [data_index] ;
right [1] += icoeff * filter->buffer [data_index + 1] ;

Expand Down Expand Up @@ -595,9 +600,10 @@ calc_output_quad (SINC_FILTER *filter, increment_t increment, increment_t start_
{ if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + 3 < filter->b_len) ;
assert (data_index + 3 < filter->b_end) ;
left [0] += icoeff * filter->buffer [data_index] ;
left [1] += icoeff * filter->buffer [data_index + 1] ;
left [2] += icoeff * filter->buffer [data_index + 2] ;
Expand All @@ -619,9 +625,10 @@ calc_output_quad (SINC_FILTER *filter, increment_t increment, increment_t start_
do
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + 3 < filter->b_len) ;
assert (data_index + 3 < filter->b_end) ;
right [0] += icoeff * filter->buffer [data_index] ;
right [1] += icoeff * filter->buffer [data_index + 1] ;
right [2] += icoeff * filter->buffer [data_index + 2] ;
Expand Down Expand Up @@ -750,9 +757,10 @@ calc_output_hex (SINC_FILTER *filter, increment_t increment, increment_t start_f
{ if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + 5 < filter->b_len) ;
assert (data_index + 5 < filter->b_end) ;
left [0] += icoeff * filter->buffer [data_index] ;
left [1] += icoeff * filter->buffer [data_index + 1] ;
left [2] += icoeff * filter->buffer [data_index + 2] ;
Expand All @@ -776,9 +784,10 @@ calc_output_hex (SINC_FILTER *filter, increment_t increment, increment_t start_f
do
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + 5 < filter->b_len) ;
assert (data_index + 5 < filter->b_end) ;
right [0] += icoeff * filter->buffer [data_index] ;
right [1] += icoeff * filter->buffer [data_index + 1] ;
right [2] += icoeff * filter->buffer [data_index + 2] ;
Expand Down Expand Up @@ -916,14 +925,16 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start
do
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
{ /*
** Duff's Device.
** See : http://en.wikipedia.org/wiki/Duff's_device
*/
assert (data_index >= 0 && data_index + channels - 1 < filter->b_len) ;
assert (data_index + channels - 1 < filter->b_end) ;
ch = channels ;
do
{ switch (ch % 8)
Expand Down Expand Up @@ -978,9 +989,10 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start
do
{ fraction = fp_to_double (filter_index) ;
indx = fp_to_int (filter_index) ;

assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;

assert (data_index >= 0 && data_index + channels - 1 < filter->b_len) ;
assert (data_index + channels - 1 < filter->b_end) ;
ch = channels ;
do
{
Expand Down

0 comments on commit 54186d5

Please sign in to comment.