From c91ca40faaf0c2658497f83c964b7af9e5452513 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 20 Feb 2018 14:06:11 -0700 Subject: [PATCH 1/2] bpo-31848: Fix broken error handling in Aifc_read.initfp() when the SSND chunk is not found (GH-5240) Initialize self._ssnd_chunk so that aifc.Error is raised as intended, not AttributeError. (cherry picked from commit 80d20b918bd8a882043c493a7f958333ecb41727) Co-authored-by: Zackery Spytz --- Lib/aifc.py | 1 + Lib/test/test_aifc.py | 8 ++++++++ Misc/ACKS | 1 + .../next/Library/2018-01-18-23-34-17.bpo-31848.M2cldy.rst | 2 ++ 4 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-01-18-23-34-17.bpo-31848.M2cldy.rst diff --git a/Lib/aifc.py b/Lib/aifc.py index e6783277a471c1..981f8010690e00 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -308,6 +308,7 @@ def initfp(self, file): else: raise Error, 'not an AIFF or AIFF-C file' self._comm_chunk_read = 0 + self._ssnd_chunk = None while 1: self._ssnd_seek_needed = 1 try: diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index d1b7dd0432579c..0152c95005ad51 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -214,6 +214,14 @@ def test_read_no_comm_chunk(self): b = io.BytesIO('FORM' + struct.pack('>L', 4) + 'AIFF') self.assertRaises(aifc.Error, aifc.open, b) + def test_read_no_ssnd_chunk(self): + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' + b += b'COMM' + struct.pack('>LhlhhLL', 38, 0, 0, 0, 0, 0, 0) + b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' + with self.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk' + ' missing'): + aifc.open(io.BytesIO(b)) + def test_read_wrong_compression_type(self): b = 'FORM' + struct.pack('>L', 4) + 'AIFC' b += 'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) diff --git a/Misc/ACKS b/Misc/ACKS index f0a115d5d55559..e02060d8dcf1bb 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1345,6 +1345,7 @@ Nicholas Spies Per Spilling Joshua Spoerri Noah Spurrier +Zackery Spytz Nathan Srebro RajGopal Srinivasan Tage Stabell-Kulo diff --git a/Misc/NEWS.d/next/Library/2018-01-18-23-34-17.bpo-31848.M2cldy.rst b/Misc/NEWS.d/next/Library/2018-01-18-23-34-17.bpo-31848.M2cldy.rst new file mode 100644 index 00000000000000..c8e61acb0b066b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-01-18-23-34-17.bpo-31848.M2cldy.rst @@ -0,0 +1,2 @@ +Fix the error handling in Aifc_read.initfp() when the SSND chunk is not found. +Patch by Zackery Spytz. From deead293bbb8e34d31b0d4d7cc75137a938198ca Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 21 Feb 2018 00:10:10 +0200 Subject: [PATCH 2/2] Use assertRaisesRegexp(). --- Lib/test/test_aifc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index 0152c95005ad51..92bbe7bc75da93 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -218,8 +218,8 @@ def test_read_no_ssnd_chunk(self): b = b'FORM' + struct.pack('>L', 4) + b'AIFC' b += b'COMM' + struct.pack('>LhlhhLL', 38, 0, 0, 0, 0, 0, 0) b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' - with self.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk' - ' missing'): + with self.assertRaisesRegexp(aifc.Error, 'COMM chunk and/or SSND chunk' + ' missing'): aifc.open(io.BytesIO(b)) def test_read_wrong_compression_type(self):