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 — C API: Check in PyTuple_SET_ITEM() and PyList_SET_ITEM() · Issue #106168 · python/cpython

C API: Check in PyTuple_SET_ITEM() and PyList_SET_ITEM() #106168

New issue
Copy link
New issue
Copy link
Closed
Closed
C API: Check in PyTuple_SET_ITEM() and PyList_SET_ITEM()#106168
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@vstinner
vstinner
opened on Jun 28, 2023
Issue body actions

The PyTuple_SET_ITEM() and PyList_SET_ITEM() functions don't check that the index is valid. It should be checked with an assertion.

Linked PRs

  • gh-106168: PyTuple_SET_ITEM() now checks the index #106164
  • gh-106168: Check allocated instead of size index bounds in PyList_SET_ITEM() #111480
  • gh-106168: Update PyList_SET_ITEM() What's New doc #111618
  • gh-106168: Revert the "size before item" setting #111683

Activity

  1. vstinner
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 28, 2023
  2. vstinner
    added 5 commits that reference this issue on Jun 28, 2023

    pythongh-106168: PyTuple_SET_ITEM() now checks the index

    4cfa2d7

    pythongh-106168: PyTuple_SET_ITEM() now checks the index

    8b53e45

    pythongh-106168: PyTuple_SET_ITEM() now checks the index

    b489af9

    pythongh-106168: PyTuple_SET_ITEM() now checks the index

    fb5b51b

    gh-106168: PyTuple_SET_ITEM() now checks the index (#106164)

    3f8483c
  3. vstinner
    closed this as completedon Jun 28, 2023
  4. scoder
    reopened this on Oct 29, 2023
  5. scoder commented on Oct 29, 2023

    @scoder
    scoder
    on Oct 29, 2023
    Contributor
    More actions

    The PyTuple_SET_ITEM() and PyList_SET_ITEM() functions don't check that the index is valid. It should be checked with an assertion.

    I oppose this statement. Their documentation says that both allow unchecked writing into the list/tuple. Adding these checks breaks their contract.

  6. scoder commented on Oct 29, 2023

    @scoder
    scoder
    on Oct 29, 2023
    Contributor
    More actions

    If you insist on adding such a check, you can assert that the index is within the currently allocated array bounds. Writing outside of that range is definitely incorrect. However, writing beyond the current size may not be.

  7. scoder
    added 2 commits that reference this issue on Oct 29, 2023

    Work around a change in CPython 3.13a1 that prevents calling PyList_S…

    8b9c854

    pythongh-106168: Check the index bound assertions in PyList_SET_ITEM(…

    77681ba
  8. vstinner
    added a commit that references this issue on Oct 30, 2023

    gh-106168: Check allocated instead of size index bounds in PyList_SET…

    940ee96
  9. vstinner
    closed this as completedon Oct 30, 2023
  10. scoder commented on Nov 1, 2023

    @scoder
    scoder
    on Nov 1, 2023
    Contributor
    More actions

    [Copying my comment from the PR here as IMHO it describes the situation quite well and thus belongs rather into the ticket discussion.]

    @vstinner wrote:

    I dislike PyList_New() + PyList_SET_ITEM() API since the list is immediately tracked by the GC and so calling gc.get_objects() can expose an invalid list object (ex: calling repr(list) can crash)

    That's why I prefer not requiring users to change the size before they set the value. Doing that would bring the list in an invalid state.

    With this change, PyList_SetItem() and PyList_SET_ITEM() become inconsistent: PyList_SetItem() uses list->ob_size, whereas PyList_SET_ITEM() uses list->allocated.

    I don't see an inconsistency here. They serve different needs, that's why we have two different functions. As you write, PyList_SetItem() decrefs the current value and replaces it with a new one. That can only safely be done within [0:size]. PyList_SET_ITEM() writes a value to an array position that does not yet have a value. That can only safely be done within [0:allocated]. Different needs, different functions, different boundary conditions.

    You could rather argue that PyList_SET_ITEM() is only really valid within [size:allocated]. Within [0:size], it would overwrite existing, owned values. However, enforcing that would probably break even more code that might just look slightly brittle but is actually working perfectly fine (because it does what you suggested, it updates the size immediately before setting the value).

    Cython uses the _SET_ITEM() functions to initialise freshly created lists and tuples, but it also calls PyList_SET_ITEM() in a fast, inlined PyList_Append() implementation:
    https://github.com/cython/cython/blob/master/Cython/Utility/Optimize.c#L29-L77
    We use this for list comprehensions, for example, where we control the list internally. As long as the next item fits into the already allocated array, we just put it there and increase the size.

  11. vstinner commented on Nov 1, 2023

    @vstinner
    vstinner
    on Nov 1, 2023
    MemberAuthor
    More actions

    I changed PyList the same way than PyTuple, but as you described, PyList is different and requires special treatement. I'm fine with #111480 that's why I merged it 👍

  12. vstinner
    added 3 commits that reference this issue on Nov 1, 2023

    pythongh-106168: Update PyList_SET_ITEM() What's New doc

    f98e3a9

    pythongh-106168: Update PyList_SET_ITEM() What's New doc

    ecfa83f

    gh-106168: Update PyList_SET_ITEM() What's New doc (#111618)

    821a7ac
  13. FullteaR
    added 2 commits that reference this issue on Nov 3, 2023

    pythongh-106168: Check allocated instead of size index bounds in PyLi…

    0582d79

    pythongh-106168: Update PyList_SET_ITEM() What's New doc (python#111618)

    e04f8aa
  14. scoder
    added a commit that references this issue on Nov 3, 2023

    pythongh-106168: Update the size only after setting the item, to avoi…

    6b19abb
  15. vstinner
    added a commit that references this issue on Nov 3, 2023

    gh-106168: Revert the "size before item" setting (#111683)

    24ddaee
  16. aisk
    added 3 commits that reference this issue on Feb 11, 2024

    pythongh-106168: Check allocated instead of size index bounds in PyLi…

    6f86bcd

    pythongh-106168: Update PyList_SET_ITEM() What's New doc (python#111618)

    c829473

    pythongh-106168: Revert the "size before item" setting (python#111683)

    1193f1d
  17. Glyphack
    added 3 commits that reference this issue on Sep 2, 2024

    pythongh-106168: Check allocated instead of size index bounds in PyLi…

    0f86605

    pythongh-106168: Update PyList_SET_ITEM() What's New doc (python#111618)

    89f8ecf

    pythongh-106168: Revert the "size before item" setting (python#111683)

    edfc396
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

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.