GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-78724: raise RuntimeError's when calling methods on non-ready Struct()'s by skirpichev · Pull Request #143643 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Jan 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: _struct.Struct: calling functions without calling __init__ results in SystemError #78724

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
skirpichev added 5 commits January 3, 2026 10:36
@skirpichev
pythongh-143379: fix UAF in struct.Struct.pack() when object modified…
a1c2603
… by dunder methods
@skirpichev
Merge branch 'master' into fix-uaf-s_pack_internal/143379
716e28c
@skirpichev
address review: implement a different solution with a mutex flag
e611953
Now modification of the Struct() while packing trigger a RuntimeError
@skirpichev
+ clean the mess
ab62167
@skirpichev
pythongh-78724: raise RuntimeError's when calling methods on non-read…
3b18427
…y Struct()'s

Calling the ``Struct.__new__()`` dunder without required argument now is
deprecated.  Calling the ``Struct.__init__()`` dunder method on
initialized object now also is deprecated.
@bedevere-app bedevere-app Bot mentioned this pull request Jan 10, 2026
_struct.Struct: calling functions without calling __init__ results in SystemError #78724
Closed
@skirpichev skirpichev mentioned this pull request Jan 10, 2026
gh-143379: fix UAF in struct.Struct.pack() when object modified by dunder methods #143382
Closed
@skirpichev

This comment was marked as outdated.

Sign in to view
@skirpichev
skirpichev marked this pull request as ready for review January 10, 2026 07:55
@bedevere-app bedevere-app Bot added the awaiting review label Jan 10, 2026
@skirpichev
skirpichev requested a review from serhiy-storchaka January 10, 2026 07:56
serhiy-storchaka
serhiy-storchaka reviewed Jan 10, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This issue is relatively independed from the concurrent packing issue, so I suggest fir it separately. We should not add deprecation in maintained versions, so I suggest to only leave checks that Struct was initialized, and add the deprecation in a separate PR.

prepare_s() should be rewritten so it only sets s_codes and other fields when it is successful.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_struct.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_struct.c Outdated
{
PyObject *self;

if (PyTuple_GET_SIZE(args) != 1

@serhiy-storchaka serhiy-storchaka Jan 10, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I do not see how can this be useful.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@skirpichev skirpichev Jan 10, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is to trigger deprecation warning in a following scenario:

>>> import struct
>>> class MyStruct(struct.Struct):
...     def __init__(self):
...         super().__init__('>h')
...         
>>> my_struct = MyStruct()
<python-input-2>:1: DeprecationWarning: Struct().__new__() has one required argument

PS: deprecation part moved to #143659.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_struct.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_struct.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Jan 10, 2026
@skirpichev
skirpichev marked this pull request as draft January 10, 2026 10:14
@bedevere-app bedevere-app Bot removed the awaiting review label Jan 10, 2026
skirpichev added 3 commits January 10, 2026 13:15
@skirpichev
Merge branch 'master' into deprecate-struct-init/78724
54dbea8
@skirpichev
revert stuff from python#143382
b6c8bed
@skirpichev
revert deprecations (TBD: a separate pr)
f542173
@skirpichev

skirpichev commented Jan 10, 2026

Copy link
Copy Markdown
Member Author

prepare_s() should be rewritten so it only sets s_codes and other fields when it is successful.

I think it's already the case. We can move a bit s_size/s_len assignments, but as far as we interested only in s_codes - I'm not sure if it's worth. There should be no failures after codes = PyMem_Malloc(...).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
address review: redo patch to use s_codes
69a5eec
@skirpichev
skirpichev marked this pull request as ready for review January 10, 2026 11:45
@bedevere-app bedevere-app Bot added the awaiting review label Jan 10, 2026
@skirpichev
skirpichev requested a review from serhiy-storchaka January 10, 2026 11:46
@skirpichev
Merge branch 'main' into deprecate-struct-init/78724
0e6af95
@serhiy-storchaka

serhiy-storchaka commented Jan 10, 2026

Copy link
Copy Markdown
Member

Well, it does not matter. It would reduce the chance of race condition, but nobody should run __init__() and pack() concurrently, there are no reasons to do this, and we should not support this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Jan 10, 2026
View reviewed changes
Comment thread Lib/test/test_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_struct.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
address review: -asserts + redo test
8ba6b33
serhiy-storchaka
serhiy-storchaka reviewed Jan 10, 2026
View reviewed changes
Comment thread Lib/test/test_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_struct.py Outdated
self.assertRaises(RuntimeError, meth, spam)
self.assertRaises(RuntimeError, S.iter_unpack, 1)
self.assertRaises(RuntimeError, S.pack, 1)
self.assertRaises(RuntimeError, S.pack_into, 1)

@serhiy-storchaka serhiy-storchaka Jan 10, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

S.pack_into(buffer, offset, v1, v2, ...)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@skirpichev skirpichev Jan 10, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

BTW, I think that argument processing in the struct's functions/methods should be refactored and transformed to the AC. E.g. S.pack_info could check required arguments before validation of the soself struct.

Though, this belongs to a separate issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@skirpichev skirpichev Jan 11, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

AC cleanup goes to #143673

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
address review: adjust tests
ea0f8e2
@skirpichev

skirpichev commented Jan 10, 2026

Copy link
Copy Markdown
Member Author

Well, it does not matter. It would reduce the chance of race condition, but nobody should run __init__() and pack() concurrently, there are no reasons to do this, and we should not support this.

I guess this belongs to the #143382 pr thread.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
skirpichev requested a review from serhiy-storchaka January 11, 2026 11:09
serhiy-storchaka
serhiy-storchaka reviewed Jan 11, 2026
View reviewed changes
Comment thread Modules/_struct.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_struct.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev added 2 commits January 11, 2026 18:21
@skirpichev
Merge branch 'master' into deprecate-struct-init/78724
c71be45
@skirpichev
address review: revert unrelated change
b61bddb
serhiy-storchaka
serhiy-storchaka approved these changes Jan 11, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM. 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jan 11, 2026
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) January 11, 2026 15:47
@serhiy-storchaka
serhiy-storchaka merged commit 515ae40 into python:main Jan 11, 2026
50 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jan 11, 2026
@miss-islington-app

miss-islington-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 11, 2026
@skirpichev @miss-islington
pythongh-78724: Raise RuntimeError's when calling methods on non-read…
660b508
…y Struct()'s (pythonGH-143643)

(cherry picked from commit 515ae40)

Co-authored-by: Sergey B Kirpichev <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

Sorry, @skirpichev and @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 515ae4078dffa0b74e5e5431462c2f4fe4563ffa 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned serhiy-storchaka Jan 11, 2026
@bedevere-app

bedevere-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

GH-143695 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jan 11, 2026
@skirpichev
skirpichev deleted the deprecate-struct-init/78724 branch January 11, 2026 18:04
@skirpichev skirpichev self-assigned this Jan 11, 2026
@bedevere-app

bedevere-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

GH-143714 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Jan 11, 2026
@skirpichev skirpichev removed their assignment Jan 11, 2026
serhiy-storchaka pushed a commit that referenced this pull request Jan 12, 2026
@miss-islington @skirpichev
[3.14] gh-78724: Raise RuntimeError's when calling methods on non-rea…
8f85adc
…dy Struct()'s (GH-143643) (GH-143695)

(cherry picked from commit 515ae40)

Co-authored-by: Sergey B Kirpichev <[email protected]>
serhiy-storchaka pushed a commit that referenced this pull request Jan 12, 2026
@skirpichev
[3.13] gh-78724: Raise RuntimeError's when calling methods on non-rea…
8fd4435
…dy Struct()'s (GH-143643) (GH-143714)

(cherry picked from commit 515ae40)
reidenong pushed a commit to reidenong/cpython that referenced this pull request Jan 12, 2026
@skirpichev @reidenong
pythongh-78724: Raise RuntimeError's when calling methods on non-read…
e3ef2fc
…y Struct()'s (pythonGH-143643)
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
@skirpichev @thunder-coding
pythongh-78724: Raise RuntimeError's when calling methods on non-read…
3ab851b
…y Struct()'s (pythonGH-143643)
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@skirpichev @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.