Skip to content

gh-151308: Avoid huge pre-allocation in wave.readframes() for crafted files#151487

Closed
iamsharduld wants to merge 1 commit into
python:mainfrom
iamsharduld:fix-wave-memory-exhaustion
Closed

gh-151308: Avoid huge pre-allocation in wave.readframes() for crafted files#151487
iamsharduld wants to merge 1 commit into
python:mainfrom
iamsharduld:fix-wave-memory-exhaustion

Conversation

@iamsharduld

Copy link
Copy Markdown
Contributor

A WAV data chunk records its size in a 4-byte header field that is not
validated against the data actually present in the file. A small,
truncated, or maliciously crafted file can therefore claim a chunk of
several gigabytes and make wave.Wave_read.readframes() pre-allocate that
much memory via a single file.read(chunksize) call, leading to a
MemoryError (or memory exhaustion) from a tiny input.

When the underlying file is seekable, this clamps each read in the internal
_Chunk.read() to the number of bytes physically available, so we never
allocate more than the file can actually provide. The data returned for
valid files is unchanged.

Non-seekable streams retain the previous behaviour, since their size can't
be probed without buffering; the realistic attack vector is a .wav file
on disk, which is fully covered.

…rafted files

A WAV data chunk records its size in a 4-byte header field that is not
validated against the data actually present in the file.  A small,
truncated, or maliciously crafted file could therefore claim a chunk of
several gigabytes and make wave.Wave_read.readframes() pre-allocate that
much memory via a single file.read(chunksize) call, leading to a
MemoryError (or memory exhaustion) from a tiny input.

When the underlying file is seekable, clamp each read in the internal
_Chunk.read() to the number of bytes physically available, so we never
allocate more than the file can actually provide.  The data returned for
valid files is unchanged.
@iamsharduld iamsharduld force-pushed the fix-wave-memory-exhaustion branch from 232ea59 to 6b72dd4 Compare June 15, 2026 08:19
@iamsharduld iamsharduld deleted the fix-wave-memory-exhaustion branch June 15, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant