tarfile: Degenerate TARs with global PAX headers are not handled correctly
未關閉
還沒有人認領這個 Issue。
stdlib
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
It appears that tarfile.TarFile fails opening PAX TAR files if the only thing they contain is the global headers, no matter if created with the module or with the tar tool. The exception raised is tarfile.ReadError: end of file header. The tar executable handles those files perfectly.
Reproducer
import subprocess
import traceback
import tarfile
import sys
import io
#
# Non-empty Python-created files work
#
filename = "non_empty_python.tar"
with tarfile.TarFile(filename,
mode="w",
format=tarfile.PAX_FORMAT,
pax_headers=dict(abc="def")
) as t:
t.addfile(tarfile.TarInfo("ghi.txt"), io.BytesIO(b"ghi"))
subprocess.check_call(["tar", "tf", filename],
stdout=subprocess.DEVNULL)
with tarfile.TarFile(filename, mode="a"):
pass
#
# Non-empty tar-created files work
#
filename = "non_empty_tar.tar"
subprocess.check_call(["/bin/bash", "-c", f"""
tar -c -H pax --pax-option='foo=bar' -f {filename} \
-P --transform 's/.*/ghi.txt/' <(echo ghi)
"""])
subprocess.check_call(["tar", "tf", filename],
stdout=subprocess.DEVNULL)
with tarfile.TarFile(filename, mode="a"):
pass
#
# Empty Python-created files break
#
filename = "empty_python.tar"
with tarfile.TarFile(filename,
mode="w",
format=tarfile.PAX_FORMAT,
pax_headers=dict(abc="def")
) as t:
pass
subprocess.check_call(["tar", "tf", filename],
stdout=subprocess.DEVNULL)
try:
with tarfile.TarFile(filename, mode="a"):
pass
except Exception:
print(f"Opening empty Python-created PAX TAR {filename!r} failed:",
file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
#
# Empty tar-created files break
#
filename = "empty_tar.tar"
subprocess.check_call(["/bin/bash", "-c", f"""
tar -c -H pax --pax-option='foo=bar' -f {filename} \
--files-from=/dev/null
"""])
subprocess.check_call(["tar", "tf", filename],
stdout=subprocess.DEVNULL)
try:
with tarfile.TarFile(filename, mode="a"):
pass
except Exception:
print(f"Opening empty tar-created PAX TAR {filename!r} failed:",
file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Linked PRs
- gh-149647
- gh-156792
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 reproducer 和 tarfile.TarFile 以附加模式開啟的路徑著手,重點關注僅包含全域 PAX 標頭的封存檔。為 Python 建立的空 PAX 封存檔和 tar 建立的空 PAX 封存檔都加入回歸測試,並驗證以模式 "a" 開啟它們時能夠成功且不會出現 ReadError。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- tooling
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100