python / python/cpython

Abort from calling `OrderedDict.setdefault` with an invalid value

Open
#132,461 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-crash
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Crash report

What happened?

It's possible to abort the interpreter by passing a class that has invalid, changing hash values to OrderedDict.setdefault:

from abc import ABCMeta
from random import randint

large_num = 2**64
class WeirdBase(ABCMeta):
  def __hash__(self):
    return randint(0, large_num)


class weird_bytes(bytes, metaclass=WeirdBase):
    pass

from collections import OrderedDict

obj = OrderedDict()

for x in range(100):
    obj.setdefault(weird_bytes, None)

Abort message:

python: Objects/odictobject.c:1036: OrderedDict_setdefault_impl: Assertion `_odict_find_node(self, key) == NULL' failed.

Program received signal SIGABRT, Aborted.

Found using fusil by @vstinner.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.14.0a6+ (heads/main:be2d2181e62, Mar 31 2025, 07:30:17) [GCC 11.4.0]

Linked PRs
  • gh-132462

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Objects/odictobject.c at the OrderedDict_setdefault_impl assertion and reproduce the crash using the Python snippet in the issue. Review linked PR gh-132462 for the work already underway; done means the invalid-changing hash case no longer aborts the interpreter.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.