shithub: libsamplerate

Download patch

ref: 2f7c74f542b58e89525cedcf7f282a0db5ff31f4
parent: 401489195c8eae5f6897ec69df5d5b36840f39a8
author: Erik de Castro Lopo <erikd@miles>
date: Sun Jul 4 23:13:29 EDT 2004

Improve checking for possible overflow in fixed point counters.

--- a/src/src_sinc.c
+++ b/src/src_sinc.c
@@ -148,7 +148,7 @@
 int
 sinc_set_converter (SRC_PRIVATE *psrc, int src_enum)
 {	SINC_FILTER *filter, temp_filter ;
-	int count ;
+	int count, bits ;
 
 	/* Quick sanity check. */
 	if (SHIFT_BITS >= sizeof (increment_t) * 8 - 1)
@@ -217,9 +217,11 @@
 
 	sinc_reset (psrc) ;
 
-	count = lrint ((filter->coeff_half_len * INT_TO_FP (1)) / FP_ONE) ;
+	count = filter->coeff_half_len ;
+	for (bits = 0 ; (1 << bits) < count ; bits++)
+		count |= (1 << bits) ;
 
-	if (abs (count - filter->coeff_half_len) >= 1)
+	if (bits + SHIFT_BITS - 1 >= (int) (sizeof (increment_t) * 8))
 		return SRC_ERR_FILTER_LEN ;
 
 	return SRC_ERR_NO_ERROR ;