Skip to content

Commit eb4b7ba

Browse files
committed
audioop_ratecv() again: settle for a sloppier upper bound that's less
obnoxious to compute and easier to explain. No compromise on safety.
1 parent b404145 commit eb4b7ba

1 file changed

Lines changed: 19 additions & 34 deletions

File tree

‎Modules/audioop.c‎

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* audioopmodule - Module to detect peak values in arrays */
33

44
#include "Python.h"
5-
#include <math.h>
65

76
#if SIZEOF_INT == 4
87
typedef int Py_Int32;
@@ -981,46 +980,32 @@ audioop_ratecv(PyObject *self, PyObject *args)
981980
/* There are len input frames, so we need (mathematically)
982981
ceiling(len*outrate/inrate) output frames, and each frame
983982
requires bytes_per_frame bytes. Computing this
984-
without spurious overflow is the challenge. */
985-
int ceiling; /* the number of output frames, eventually */
983+
without spurious overflow is the challenge; we can
984+
settle for a reasonable upper bound, though. */
985+
int ceiling; /* the number of output frames */
986986
int nbytes; /* the number of output bytes needed */
987987
int q = len / inrate;
988-
int r = len - q * inrate;
989-
/* Now len = q * inrate + r exactly, so
990-
len*outrate/inrate =
991-
(q*inrate+r)*outrate/inrate =
992-
(q*inrate*outrate + r*outrate)/inrate =
993-
q*outrate + r*outrate/inrate exactly.
994-
q*outrate is an exact integer, so the ceiling we're after is
995-
q*outrate + ceiling(r*outrate/inrate). */
996-
ceiling = q * outrate;
997-
if (ceiling / outrate != q) {
998-
PyErr_SetString(PyExc_MemoryError,
999-
"not enough memory for output buffer");
1000-
goto exit;
1001-
}
1002-
/* Since r = len % inrate, in particular r < inrate. So
1003-
r * outrate / inrate = (r / inrate) * outrate < outrate,
1004-
so ceiling(r * outrate / inrate) <= outrate: the final
1005-
result fits in an int -- it can't overflow. */
1006-
assert(r < inrate);
1007-
q = (int)ceil((double)r * (double)outrate / (double)inrate);
1008-
assert(q <= outrate);
1009-
ceiling += q;
1010-
if (ceiling < 0) {
1011-
PyErr_SetString(PyExc_MemoryError,
1012-
"not enough memory for output buffer");
1013-
goto exit;
1014-
}
988+
/* Now len = q * inrate + r exactly (with r = len % inrate),
989+
and this is less than q * inrate + inrate = (q+1)*inrate.
990+
So a reasonable upper bound on len*outrate/inrate is
991+
((q+1)*inrate)*outrate/inrate =
992+
(q+1)*outrate.
993+
*/
994+
ceiling = (q+1) * outrate;
1015995
nbytes = ceiling * bytes_per_frame;
1016-
if (nbytes / bytes_per_frame != ceiling) {
996+
/* See whether anything overflowed; if not, get the space. */
997+
if (q+1 < 0 ||
998+
ceiling / outrate != q+1 ||
999+
nbytes / bytes_per_frame != ceiling)
1000+
str = NULL;
1001+
else
1002+
str = PyString_FromStringAndSize(NULL, nbytes);
1003+
1004+
if (str == NULL) {
10171005
PyErr_SetString(PyExc_MemoryError,
10181006
"not enough memory for output buffer");
10191007
goto exit;
10201008
}
1021-
str = PyString_FromStringAndSize(NULL, nbytes);
1022-
if (str == NULL)
1023-
goto exit;
10241009
}
10251010
ncp = PyString_AsString(str);
10261011

0 commit comments

Comments
 (0)