[Python-checkins] gh-96821: Fix undefined behaviour in `audioop.c` (#96923)
mdickinson
webhook-mailer at python.org
Mon Oct 10 12:12:45 EDT 2022
https://github.com/python/cpython/commit/553d3c10172254b190078c50eb9f8e60522c8f41
commit: 553d3c10172254b190078c50eb9f8e60522c8f41
branch: main
author: Matthias Görgens <matthias.goergens at gmail.com>
committer: mdickinson <dickinsm at gmail.com>
date: 2022-10-10T17:12:29+01:00
summary:
gh-96821: Fix undefined behaviour in `audioop.c` (#96923)
* gh-96821: Fix undefined behaviour in `audioop.c`
Left-shifting negative numbers is undefined behaviour.
Fortunately, multiplication works just as well, is defined behaviour,
and gets compiled to the same machine code as before by optimizing
compilers.
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2022-09-19-03-35-01.gh-issue-96821.izK6JA.rst
M Modules/audioop.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-19-03-35-01.gh-issue-96821.izK6JA.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-19-03-35-01.gh-issue-96821.izK6JA.rst
new file mode 100644
index 000000000000..73d0c76f0297
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-19-03-35-01.gh-issue-96821.izK6JA.rst
@@ -0,0 +1 @@
+Fix undefined behaviour in ``audioop.c``.
diff --git a/Modules/audioop.c b/Modules/audioop.c
index d74e634ac448..c29a3e8df409 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -59,6 +59,8 @@ static const int16_t seg_uend[8] = {
static int16_t
search(int16_t val, const int16_t *table, int size)
{
+ assert(0 <= size);
+ assert(size < INT16_MAX);
int i;
for (i = 0; i < size; i++) {
@@ -170,6 +172,7 @@ st_14linear2ulaw(int16_t pcm_val) /* 2's complement (14-bit range) */
if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
+ assert(seg >= 0);
uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
return (uval ^ mask);
}
@@ -300,13 +303,13 @@ static const int stepsizeTable[89] = {
#ifdef WORDS_BIGENDIAN
#define GETINT24(cp, i) ( \
((unsigned char *)(cp) + (i))[2] + \
- (((unsigned char *)(cp) + (i))[1] << 8) + \
- (((signed char *)(cp) + (i))[0] << 16) )
+ (((unsigned char *)(cp) + (i))[1] * (1 << 8)) + \
+ (((signed char *)(cp) + (i))[0] * (1 << 16)) )
#else
#define GETINT24(cp, i) ( \
((unsigned char *)(cp) + (i))[0] + \
- (((unsigned char *)(cp) + (i))[1] << 8) + \
- (((signed char *)(cp) + (i))[2] << 16) )
+ (((unsigned char *)(cp) + (i))[1] * (1 << 8)) + \
+ (((signed char *)(cp) + (i))[2] * (1 << 16)) )
#endif
@@ -347,10 +350,10 @@ static const int stepsizeTable[89] = {
} while(0)
-#define GETSAMPLE32(size, cp, i) ( \
- (size == 1) ? (int)GETINT8((cp), (i)) << 24 : \
- (size == 2) ? (int)GETINT16((cp), (i)) << 16 : \
- (size == 3) ? (int)GETINT24((cp), (i)) << 8 : \
+#define GETSAMPLE32(size, cp, i) ( \
+ (size == 1) ? (int)GETINT8((cp), (i)) * (1 << 24) : \
+ (size == 2) ? (int)GETINT16((cp), (i)) * (1 << 16) : \
+ (size == 3) ? (int)GETINT24((cp), (i)) * (1 << 8) : \
(int)GETINT32((cp), (i)))
#define SETSAMPLE32(size, cp, i, val) do { \
@@ -1558,7 +1561,7 @@ audioop_ulaw2lin_impl(PyObject *module, Py_buffer *fragment, int width)
cp = fragment->buf;
for (i = 0; i < fragment->len*width; i += width) {
- int val = st_ulaw2linear16(*cp++) << 16;
+ int val = st_ulaw2linear16(*cp++) * (1 << 16);
SETSAMPLE32(width, ncp, i, val);
}
return rv;
@@ -1632,7 +1635,7 @@ audioop_alaw2lin_impl(PyObject *module, Py_buffer *fragment, int width)
cp = fragment->buf;
for (i = 0; i < fragment->len*width; i += width) {
- val = st_alaw2linear16(*cp++) << 16;
+ val = st_alaw2linear16(*cp++) * (1 << 16);
SETSAMPLE32(width, ncp, i, val);
}
return rv;
@@ -1757,7 +1760,7 @@ audioop_lin2adpcm_impl(PyObject *module, Py_buffer *fragment, int width,
/* Step 6 - Output value */
if ( bufferstep ) {
- outputbuffer = (delta << 4) & 0xf0;
+ outputbuffer = (delta * (1 << 4)) & 0xf0;
} else {
*ncp++ = (delta & 0x0f) | outputbuffer;
}
@@ -1875,7 +1878,7 @@ audioop_adpcm2lin_impl(PyObject *module, Py_buffer *fragment, int width,
step = stepsizeTable[index];
/* Step 6 - Output value */
- SETSAMPLE32(width, ncp, i, valpred << 16);
+ SETSAMPLE32(width, ncp, i, valpred * (1 << 16));
}
rv = Py_BuildValue("(O(ii))", str, valpred, index);
More information about the Python-checkins
mailing list