GitHub

Original file line numberDiff line numberDiff line change

@@ -1160,25 +1160,16 @@ audioop_ratecv(PyObject *self, PyObject *args)

11601160

ceiling(len*outrate/inrate) output frames, and each frame

11611161

requires bytes_per_frame bytes. Computing this

11621162

without spurious overflow is the challenge; we can

1163-

settle for a reasonable upper bound, though. */

1164-

int ceiling; /* the number of output frames */

1165-

int nbytes; /* the number of output bytes needed */

1166-

int q = len / inrate;

1167-

/* Now len = q * inrate + r exactly (with r = len % inrate),

1168-

and this is less than q * inrate + inrate = (q+1)*inrate.

1169-

So a reasonable upper bound on len*outrate/inrate is

1170-

((q+1)*inrate)*outrate/inrate =

1171-

(q+1)*outrate.

1172-

*/

1173-

ceiling = (q+1) * outrate;

1174-

nbytes = ceiling * bytes_per_frame;

1175-

/* See whether anything overflowed; if not, get the space. */

1176-

if (q+1 < 0 ||

1177-

ceiling / outrate != q+1 ||

1178-

nbytes / bytes_per_frame != ceiling)

1163+

settle for a reasonable upper bound, though, in this

1164+

case ceiling(len/inrate) * outrate. */

1165+
1166+

/* compute ceiling(len/inrate) without overflow */

1167+

int q = len > 0 ? 1 + (len - 1) / inrate : 0;

1168+

if (outrate > INT_MAX / q / bytes_per_frame)

11791169

str = NULL;

11801170

else

1181-

str = PyBytes_FromStringAndSize(NULL, nbytes);

1171+

str = PyBytes_FromStringAndSize(NULL,

1172+

q * outrate * bytes_per_frame);

11821173
11831174

if (str == NULL) {

11841175

PyErr_SetString(PyExc_MemoryError,

Read the original on github.com ↗