Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type_new doesn't allocate space for sentinal slot #45578

Closed
Rhamphoryncus mannequin opened this issue Oct 5, 2007 · 5 comments
Closed

type_new doesn't allocate space for sentinal slot #45578

Rhamphoryncus mannequin opened this issue Oct 5, 2007 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@Rhamphoryncus
Copy link
Mannequin

Rhamphoryncus mannequin commented Oct 5, 2007

BPO 1237
Nosy @gvanrossum

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2007-10-05.18:33:31.760>
created_at = <Date 2007-10-05.00:00:41.783>
labels = ['interpreter-core', 'invalid']
title = "type_new doesn't allocate space for sentinal slot"
updated_at = <Date 2007-10-05.18:33:31.759>
user = 'https://bugs.python.org/Rhamphoryncus'

bugs.python.org fields:

activity = <Date 2007-10-05.18:33:31.759>
actor = 'gvanrossum'
assignee = 'none'
closed = True
closed_date = <Date 2007-10-05.18:33:31.760>
closer = 'gvanrossum'
components = ['Interpreter Core']
creation = <Date 2007-10-05.00:00:41.783>
creator = 'Rhamphoryncus'
dependencies = []
files = []
hgrepos = []
issue_num = 1237
keywords = []
message_count = 5.0
messages = ['56231', '56239', '56241', '56243', '56244']
nosy_count = 2.0
nosy_names = ['gvanrossum', 'Rhamphoryncus']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1237'
versions = []

@Rhamphoryncus
Copy link
Mannequin Author

Rhamphoryncus mannequin commented Oct 5, 2007

type_new() allocates the exact number of slots it's going to use, but
various other functions assume there's one more slot with a NULL name
field serving as a sentinel. I'm unsure why it doesn't normally crash.

@Rhamphoryncus Rhamphoryncus mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 5, 2007
@gvanrossum
Copy link
Member

Can you be more specific as to on which line number the questionable
allocation happens, and which functions are depending on there being one
extra slot?

@Rhamphoryncus
Copy link
Mannequin Author

Rhamphoryncus mannequin commented Oct 5, 2007

typeobject.c:1842:type_new
type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
nslots may be 0.

typeobject.c:1966:type_new assigns this just-past-the-end address to
tp_members
type->tp_members = PyHeapType_GET_MEMBERS(et);

type_new later calls PyType_Ready, which calls add_members.
typeobject.c:3062:add_members
	for (; memb->name != NULL; memb++) {

Interestingly, traverse_slots and clear_slots both use Py_Size rather
than name != NULL (so I was wrong about the extent of the problem.)
Both seem only to be used for heap types. add_members is used by both
heap types and static C types, so it needs to handle both behaviours.

One possible (if ugly) solution would be to switch iteration methods
depending on if Py_Size() is 0 or not, making sure type_new sets
tp_members to NULL if Py_Size() is 0.

@gvanrossum
Copy link
Member

Are you sure you're not missing the +1 on line 440 in PyType_GenericAlloc()?

@Rhamphoryncus
Copy link
Mannequin Author

Rhamphoryncus mannequin commented Oct 5, 2007

Ugh, you're right.

I refactored PyType_GenericAlloc out of my fork, which is why I got a crash.

Sorry for wasting your time.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant