Skip to content

Heap buffer overflow in _PyTokenizer_ensure_utf8 #144872

Description

@AdamKorcz

Bug report

Bug description:

OSS-Fuzz has found a heap buffer overflow in _PyTokenizer_ensure_utf8. Link to OSS-Fuzz bug report.

The root cause is that valid_utf8() in Parser/tokenizer/helpers.c checks continuation bytes in reverse order thus reader s[expected] before s[1] on these lines:

for (; expected; expected--)
if (s[expected] < 0x80 || s[expected] >= 0xC0)
return 0;

When a multi-byte UTF-8 sequence is truncated - such as a 3-byte lead \xEA followed immediately by a null terminator - the backward loop reads past the end of the valid data before encountering the null byte that would stop it.

This is not a security-critical issue.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. added a commit that references this issue on Feb 26, 2026
  2. added a commit that references this issue on Feb 26, 2026
  3. added a commit that references this issue on Feb 26, 2026
  4. added a commit that references this issue on Feb 26, 2026
  5. added a commit that references this issue on Feb 28, 2026
  6. added a commit that references this issue on Mar 2, 2026
  7. added a commit that references this issue on Mar 2, 2026
  8. pablogsal commented on Mar 2, 2026

    @pablogsal
    Member

    Thanks a lot for the report @AdamKorcz !

  9. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions