Skip to content

Commit 11bb2cd

Browse files
committed
Issue python#8674: fix another bogus overflow check in audioop module.
1 parent 5e13e29 commit 11bb2cd

1 file changed

Lines changed: 8 additions & 17 deletions

File tree

‎Modules/audioop.c‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,25 +1153,16 @@ audioop_ratecv(PyObject *self, PyObject *args)
11531153
ceiling(len*outrate/inrate) output frames, and each frame
11541154
requires bytes_per_frame bytes. Computing this
11551155
without spurious overflow is the challenge; we can
1156-
settle for a reasonable upper bound, though. */
1157-
int ceiling; /* the number of output frames */
1158-
int nbytes; /* the number of output bytes needed */
1159-
int q = len / inrate;
1160-
/* Now len = q * inrate + r exactly (with r = len % inrate),
1161-
and this is less than q * inrate + inrate = (q+1)*inrate.
1162-
So a reasonable upper bound on len*outrate/inrate is
1163-
((q+1)*inrate)*outrate/inrate =
1164-
(q+1)*outrate.
1165-
*/
1166-
ceiling = (q+1) * outrate;
1167-
nbytes = ceiling * bytes_per_frame;
1168-
/* See whether anything overflowed; if not, get the space. */
1169-
if (q+1 < 0 ||
1170-
ceiling / outrate != q+1 ||
1171-
nbytes / bytes_per_frame != ceiling)
1156+
settle for a reasonable upper bound, though, in this
1157+
case ceiling(len/inrate) * outrate. */
1158+
1159+
/* compute ceiling(len/inrate) without overflow */
1160+
int q = len > 0 ? 1 + (len - 1) / inrate : 0;
1161+
if (outrate > INT_MAX / q / bytes_per_frame)
11721162
str = NULL;
11731163
else
1174-
str = PyString_FromStringAndSize(NULL, nbytes);
1164+
str = PyString_FromStringAndSize(NULL,
1165+
q * outrate * bytes_per_frame);
11751166

11761167
if (str == NULL) {
11771168
PyErr_SetString(PyExc_MemoryError,

0 commit comments

Comments
 (0)