tarfile: Degenerate TARs with global PAX headers are not handled correctly
未关闭
还没有人认领这个 Issue。
stdlib
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- 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