History

Add assertions to detect OOB access in SINC

Alexander Grund committed 7 years ago

Commit 54186d51 parent e2093491 file · +31 −19Browse files

Unified · Split

src/src_sinc.c+31−19
@@ -6,6 +6,7 @@ · 5 unmodified lines
66 ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
77 */
88
9+#include <assert.h>
910 #include <stdio.h>
1011 #include <stdlib.h>
1112 #include <string.h>
@@ -299,9 +300,10 @@ calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t star · 287 unmodified lines
299300 { if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
300301 { fraction = fp_to_double (filter_index) ;
301302 indx = fp_to_int (filter_index) ;
302−
303+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
303304 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
304−
305+ assert (data_index >= 0 && data_index < filter->b_len) ;
306+ assert (data_index < filter->b_end) ;
305307 left += icoeff * filter->buffer [data_index] ;
306308 } ;
307309
@@ -320,9 +322,10 @@ calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t star · 12 unmodified lines
320322 do
321323 { fraction = fp_to_double (filter_index) ;
322324 indx = fp_to_int (filter_index) ;
323−
325+ assert (indx < filter->coeff_half_len + 2) ;
324326 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
325−
327+ assert (data_index >= 0 && data_index < filter->b_len) ;
328+ assert (data_index < filter->b_end) ;
326329 right += icoeff * filter->buffer [data_index] ;
327330
328331 filter_index -= increment ;
@@ -446,9 +449,10 @@ calc_output_stereo (SINC_FILTER *filter, increment_t increment, increment_t star · 117 unmodified lines
446449 { if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
447450 { fraction = fp_to_double (filter_index) ;
448451 indx = fp_to_int (filter_index) ;
449−
452+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
450453 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
451−
454+ assert (data_index >= 0 && data_index + 1 < filter->b_len) ;
455+ assert (data_index + 1 < filter->b_end) ;
452456 left [0] += icoeff * filter->buffer [data_index] ;
453457 left [1] += icoeff * filter->buffer [data_index + 1] ;
454458 } ;
@@ -468,9 +472,10 @@ calc_output_stereo (SINC_FILTER *filter, increment_t increment, increment_t star · 13 unmodified lines
468472 do
469473 { fraction = fp_to_double (filter_index) ;
470474 indx = fp_to_int (filter_index) ;
471−
475+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
472476 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
473−
477+ assert (data_index >= 0 && data_index + 1 < filter->b_len) ;
478+ assert (data_index + 1 < filter->b_end) ;
474479 right [0] += icoeff * filter->buffer [data_index] ;
475480 right [1] += icoeff * filter->buffer [data_index + 1] ;
476481
@@ -595,9 +600,10 @@ calc_output_quad (SINC_FILTER *filter, increment_t increment, increment_t start_ · 118 unmodified lines
595600 { if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
596601 { fraction = fp_to_double (filter_index) ;
597602 indx = fp_to_int (filter_index) ;
598−
603+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
599604 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
600−
605+ assert (data_index >= 0 && data_index + 3 < filter->b_len) ;
606+ assert (data_index + 3 < filter->b_end) ;
601607 left [0] += icoeff * filter->buffer [data_index] ;
602608 left [1] += icoeff * filter->buffer [data_index + 1] ;
603609 left [2] += icoeff * filter->buffer [data_index + 2] ;
@@ -619,9 +625,10 @@ calc_output_quad (SINC_FILTER *filter, increment_t increment, increment_t start_ · 15 unmodified lines
619625 do
620626 { fraction = fp_to_double (filter_index) ;
621627 indx = fp_to_int (filter_index) ;
622−
628+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
623629 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
624−
630+ assert (data_index >= 0 && data_index + 3 < filter->b_len) ;
631+ assert (data_index + 3 < filter->b_end) ;
625632 right [0] += icoeff * filter->buffer [data_index] ;
626633 right [1] += icoeff * filter->buffer [data_index + 1] ;
627634 right [2] += icoeff * filter->buffer [data_index + 2] ;
@@ -750,9 +757,10 @@ calc_output_hex (SINC_FILTER *filter, increment_t increment, increment_t start_f · 122 unmodified lines
750757 { if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
751758 { fraction = fp_to_double (filter_index) ;
752759 indx = fp_to_int (filter_index) ;
753−
760+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
754761 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
755−
762+ assert (data_index >= 0 && data_index + 5 < filter->b_len) ;
763+ assert (data_index + 5 < filter->b_end) ;
756764 left [0] += icoeff * filter->buffer [data_index] ;
757765 left [1] += icoeff * filter->buffer [data_index + 1] ;
758766 left [2] += icoeff * filter->buffer [data_index + 2] ;
@@ -776,9 +784,10 @@ calc_output_hex (SINC_FILTER *filter, increment_t increment, increment_t start_f · 17 unmodified lines
776784 do
777785 { fraction = fp_to_double (filter_index) ;
778786 indx = fp_to_int (filter_index) ;
779−
787+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
780788 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
781−
789+ assert (data_index >= 0 && data_index + 5 < filter->b_len) ;
790+ assert (data_index + 5 < filter->b_end) ;
782791 right [0] += icoeff * filter->buffer [data_index] ;
783792 right [1] += icoeff * filter->buffer [data_index + 1] ;
784793 right [2] += icoeff * filter->buffer [data_index + 2] ;
@@ -916,7 +925,7 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start · 131 unmodified lines
916925 do
917926 { fraction = fp_to_double (filter_index) ;
918927 indx = fp_to_int (filter_index) ;
919−
928+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
920929 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
921930
922931 if (data_index >= 0) /* Avoid underflow access to filter->buffer. */
@@ -924,6 +933,8 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start · 1 unmodified line
924933 ** Duff's Device.
925934 ** See : http://en.wikipedia.org/wiki/Duff's_device
926935 */
936+ assert (data_index >= 0 && data_index + channels - 1 < filter->b_len) ;
937+ assert (data_index + channels - 1 < filter->b_end) ;
927938 ch = channels ;
928939 do
929940 { switch (ch % 8)
@@ -978,9 +989,10 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start · 48 unmodified lines
978989 do
979990 { fraction = fp_to_double (filter_index) ;
980991 indx = fp_to_int (filter_index) ;
981−
992+ assert (indx >= 0 && indx + 1 < filter->coeff_half_len + 2) ;
982993 icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
983−
994+ assert (data_index >= 0 && data_index + channels - 1 < filter->b_len) ;
995+ assert (data_index + channels - 1 < filter->b_end) ;
984996 ch = channels ;
985997 do
986998 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t