ElementTree slice assignment with start greater than stop duplicates a child without owning its second reference
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start in Modules/_elementtree.c near lines 1976-1977, where PySlice_AdjustIndices runs, then compare the insertion loop at about 2026-2030 with the replacement loop at 2033-2037. Check the pure-Python behaviour in Lib/xml/etree/ElementTree.py, which the issue says gives the expected result. Done means the reproducer prints ['a0', 'a1', 'a2', 'a3', 'a4', 'X', 'a5', 'a6'], the refcount of a2 is unchanged, and a regression test is added to Lib/test/test_xml_etree.py.
索引モデルが issue の本文から書いたものです。
説明
Bug report
Assigning to a step-1 slice with start > stop should insert at start, as a list does. The C Element implementation shifts from stop but writes from start. This duplicates one child without incrementing its reference count and overwrites another child's slot without releasing its reference.
import sys
import xml.etree.ElementTree as ET
kids = [ET.Element('a%d' % i) for i in range(7)]
e = ET.Element('r')
e[:] = kids
before = sys.getrefcount(kids[2])
e[5:2] = [ET.Element('X')]
tags = [c.tag for c in e]
print(tags, 'refcount change of a2:', sys.getrefcount(kids[2]) - before)
Recorded on 3.15.2 and main:
['a0', 'a1', 'a2', 'a2', 'a3', 'X', 'a5', 'a6'] refcount change of a2: 0
Expected, also obtained with the pure-Python Element implementation:
['a0', 'a1', 'a2', 'a3', 'a4', 'X', 'a5', 'a6']
The duplicate a2 has no additional owned reference. Releasing the parent therefore decrements it twice, potentially freeing it while kids still holds it. The record confirms this lifetime error separately; the reproducer above shows the wrong slots and missing reference without adding an allocator-reuse demonstration.
Cause: Modules/_elementtree.c:1976-1977 uses PySlice_AdjustIndices without normalizing a backward empty slice. The insertion loop at :2026-2030 shifts from stop, whereas replacement at :2033-2037 starts at start. Normalize stop to start for a positive step with stop < start, as list slice assignment does. No reentrancy or threads are required.
CPython versions tested on:
3.15, CPython main branch
Operating systems tested on:
Linux
Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude and Codex) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: textlibs-etree-slice-backward-dup
Linked PRs
- gh-161736
- 主要言語
- Python
- スター
- 78.2k
- フォーク
- 36.6k
- 平均マージ
- 19時間 57分
- マージ済み PR(30日)
- 630
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
stdlib type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
extension-modules stdlib topic-XML type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
python/cpython#161807 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
python/cpython#161788 · コメント 7 件 ·
メンテナーはふだん 1 日以内に返信
-
extension-modules topic-IO type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
cpp-best-practices/project_validator#267 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
メンテナーはふだん 1 日以内に返信
-
[Bug] Sending a Telegram message chain twice duplicates mentions対応中かも @123XieC が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
AstrBotDevs/AstrBot#11125 ·
メンテナーはふだん 1 日以内に返信
-
backend bug priority: high
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nooriegarg/ContribIQ#73 ·
メンテナーはふだん 1 日以内に返信
-
bug p3 security
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信