Skip to content

Commit d4367c2

Browse files
committed
Merge r81080 from the python2.6 branch:
Issue python#8674: fix another bogus overflow check in audioop module.
1 parent 192b714 commit d4367c2

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

Modules/audioop.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,25 +1150,16 @@ audioop_ratecv(PyObject *self, PyObject *args)
11501150
ceiling(len*outrate/inrate) output frames, and each frame
11511151
requires bytes_per_frame bytes. Computing this
11521152
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)
11691159
str = NULL;
11701160
else
1171-
str = PyString_FromStringAndSize(NULL, nbytes);
1161+
str = PyString_FromStringAndSize(NULL,
1162+
q * outrate * bytes_per_frame);
11721163

11731164
if (str == NULL) {
11741165
PyErr_SetString(PyExc_MemoryError,

0 commit comments

Comments
 (0)