@@ -1150,25 +1150,16 @@ audioop_ratecv(PyObject *self, PyObject *args)
1150
1150
ceiling(len*outrate/inrate) output frames, and each frame
1151
1151
requires bytes_per_frame bytes. Computing this
1152
1152
without spurious overflow is the challenge; we can
1153
- settle for a reasonable upper bound, though. */
1154
- int ceiling ; /* the number of output frames */
1155
- int nbytes ; /* the number of output bytes needed */
1156
- int q = len / inrate ;
1157
- /* Now len = q * inrate + r exactly (with r = len % inrate),
1158
- and this is less than q * inrate + inrate = (q+1)*inrate.
1159
- So a reasonable upper bound on len*outrate/inrate is
1160
- ((q+1)*inrate)*outrate/inrate =
1161
- (q+1)*outrate.
1162
- */
1163
- ceiling = (q + 1 ) * outrate ;
1164
- nbytes = ceiling * bytes_per_frame ;
1165
- /* See whether anything overflowed; if not, get the space. */
1166
- if (q + 1 < 0 ||
1167
- ceiling / outrate != q + 1 ||
1168
- nbytes / bytes_per_frame != ceiling )
1153
+ settle for a reasonable upper bound, though, in this
1154
+ case ceiling(len/inrate) * outrate. */
1155
+
1156
+ /* compute ceiling(len/inrate) without overflow */
1157
+ int q = len > 0 ? 1 + (len - 1 ) / inrate : 0 ;
1158
+ if (outrate > INT_MAX / q / bytes_per_frame )
1169
1159
str = NULL ;
1170
1160
else
1171
- str = PyString_FromStringAndSize (NULL , nbytes );
1161
+ str = PyString_FromStringAndSize (NULL ,
1162
+ q * outrate * bytes_per_frame );
1172
1163
1173
1164
if (str == NULL ) {
1174
1165
PyErr_SetString (PyExc_MemoryError ,
0 commit comments