Skip to content

Fix SpooledTemporaryFile.readinto/readinto1 reading twice before rollover - #1215

Merged
agronholm merged 9 commits into
agronholm:masterfrom
c-tonneslan:fix-spooled-readinto
Jul 19, 2026
Merged

agronholm merged 9 commits into
agronholm:masterfrom
c-tonneslan:fix-spooled-readinto

Conversation

@c-tonneslan

Copy link
Copy Markdown
Contributor

Changes

Before rollover, SpooledTemporaryFile.readinto and readinto1 read from the underlying file object but discard the result and fall through to super().readinto(b), which reads a second time. The buffer is overwritten by the second read and the file position advances twice, so the caller silently loses the first chunk of data. readinto1 additionally called self._fp.readinto instead of self._fp.readinto1.

Every other read method on the class (read, read1, readline, readlines) already return from the non-rolled branch; these two were missing the return.

async with SpooledTemporaryFile(max_size=1024, mode="w+b") as f:
    await f.write(b"hello world")
    await f.seek(0)
    buf = bytearray(5)
    await f.readinto(buf)
    # before: buf == b" worl", f.tell() == 10
    # after:  buf == b"hello", f.tell() == 5

Only triggers before rollover (while the spooled data is still in memory), which is why it went unnoticed.

Checklist

  • You've added tests (in tests/) which would fail without your patch
  • You've updated the documentation (in docs/), in case of behavior changes or new features
  • You've added a new changelog entry (in docs/versionhistory.rst).

…over

Signed-off-by: c-tonneslan <cst0520@gmail.com>
Signed-off-by: c-tonneslan <cst0520@gmail.com>

@tapetersen tapetersen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for a good PR.

Comment thread docs/versionhistory.rst Resolved
Comment thread tests/test_tempfile.py Resolved
@agronholm agronholm added this to the 4.15 milestone Jul 17, 2026
@agronholm

Copy link
Copy Markdown
Owner

I was contacted by Rohit Sriram, a security researcher who ran Faultmark against AnyIO's code. This was the only issue the software found.

@agronholm
agronholm merged commit ea546ef into agronholm:master Jul 19, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants