Skip to content

Conversation

@superboy-zjc
Copy link

Validate the types returned from _weak_cache.get() and _weak_cache.setdefault() to prevent type confusion when a ZoneInfo subclass provides a misbehaving cache implementation.

Fixes gh-142781.

Validate the types returned from _weak_cache.get() and
_weak_cache.setdefault() to prevent type confusion when a ZoneInfo
subclass provides a misbehaving cache implementation.
@python-cla-bot
Copy link

python-cla-bot bot commented Dec 18, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

}

if (instance != Py_None && !PyObject_TypeCheck(instance, type)) {
const char *e = "%s._weak_cache.get() returned %s, expected %s";
Copy link
Member

Choose a reason for hiding this comment

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

No need to have a temporary variable. In addition, avoid exposing implementation details about weak_cache here. Instead, I would simply say that the instance is incorrect (something like "expecting a %T, got %T")

Comment on lines +1 to +3
Fix a type confusion in zoneinfo where a malicious override of _weak_cache
in a zoneinfo subclass can cause memory corruption and crash the
interpreter.
Copy link
Member

Choose a reason for hiding this comment

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

This entry should be rewritten, it doesn't help for the end user. It's not necessarily a "malicious" override, it could be accidental as well. I would rather have something like

:mod:`zoneinfo`: fix a crash when instantiating :class:`~zoneinfo.Zoneinfo`
objects for which the internal class-level cache is consistent.

class CZoneInfoCacheTest(ZoneInfoCacheTest):
module = c_zoneinfo

def test_weak_cache_get_type_confusion(self):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def test_weak_cache_get_type_confusion(self):
def test_inconsistent_weak_cache_get(self):

with self.assertRaises(TypeError):
EvilZoneInfo("America/Los_Angeles")

def test_weak_cache_setdefault_type_confusion(self):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def test_weak_cache_setdefault_type_confusion(self):
def test_inconsistent_weak_cache_setdefault(self):

EvilZoneInfo("America/Los_Angeles")

def test_weak_cache_setdefault_type_confusion(self):
class EvilCache:
Copy link
Member

Choose a reason for hiding this comment

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

Rename EvilCache with Cache and EvilZoneInfo by ZI only. In addition, create _weak_cache when instantiating the class:

class ZI(self.class):
    _weak_cache = Cache()

Then, check the TypeError message.

@bedevere-app
Copy link

bedevere-app bot commented Dec 27, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

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.

Type confusion in zoneinfo_ZoneInfo_impl via overridden cache setdefault()

2 participants