Skip to content

gh-149578: Fix tarfile.open failing on PAX archives with only global headers - #149647

Merged
ethanfurman merged 2 commits into
python:mainfrom
ShadiBahaa:fix-tarfile-pax-global-header-eof
Sep 1, 2026
Merged

gh-149578: Fix tarfile.open failing on PAX archives with only global headers#149647
ethanfurman merged 2 commits into
python:mainfrom
ShadiBahaa:fix-tarfile-pax-global-header-eof

Conversation

@ShadiBahaa

Copy link
Copy Markdown

Summary

Fix tarfile.open() raising ReadError when opening a PAX format tar archive that contains only global headers and no regular file members.

Root cause: In TarInfo._proc_pax(), after processing a global header (XGLTYPE), the code tries to read the next header. If the archive has no file entries after the global header, this read encounters the end-of-archive marker and raises EOFHeaderError. This exception was being caught by the generic except HeaderError clause and converted to SubsequentHeaderError, which both TarFile.next() and the append-mode initialization loop treat as a fatal error.

Fix: Catch EOFHeaderError separately before the generic HeaderError handler. For global headers (XGLTYPE), let the EOFHeaderError propagate so callers handle end-of-archive normally. For extended headers (XHDTYPE), a following file entry is mandatory, so the error is still converted to SubsequentHeaderError.

Test plan

  • Added test_pax_global_header_empty_archive in PaxWriteTest that:
    • Creates a PAX archive with global headers but no file entries
    • Verifies the archive can be opened for reading and global headers are preserved
    • Verifies the archive can be opened in append mode
    • Verifies appending a file works and global headers are preserved afterward

…lobal headers

When a PAX format tar archive contains only global headers and no
regular members, tarfile.open() raised ReadError because the
EOFHeaderError from reaching end-of-archive after the global header
was being caught and converted to SubsequentHeaderError in
TarInfo._proc_pax(). This prevented the caller from handling
end-of-archive normally.

Fix by letting EOFHeaderError propagate when processing a global
header (XGLTYPE), while still treating it as a SubsequentHeaderError
for extended headers (XHDTYPE) where a following file entry is
mandatory.
@ShadiBahaa
ShadiBahaa requested a review from ethanfurman as a code owner May 10, 2026 18:20
@python-cla-bot

python-cla-bot Bot commented May 10, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@spbnick

spbnick commented May 11, 2026

Copy link
Copy Markdown

Wow, that was fast, thank you, Shadi!

@ShadiBahaa

Copy link
Copy Markdown
Author

Very Welcome @spbnick

@picnixz

picnixz commented May 13, 2026

Copy link
Copy Markdown
Member

Please, if you use an LLM, first read our policy against them and mention their usage. The summary is entirely AI-generated and contains unnecessary information that does not help reviewers.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 19, 2026
@ethanfurman
ethanfurman merged commit 3ccb1f8 into python:main Sep 1, 2026
54 checks passed
@ethanfurman ethanfurman added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 1, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @ShadiBahaa for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Sep 1, 2026

Copy link
Copy Markdown

GH-156792 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants