Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tarfile.TarFile.next() crashes on empty tar files #91078

Closed
progval mannequin opened this issue Mar 4, 2022 · 2 comments · Fixed by #91850
Closed

tarfile.TarFile.next() crashes on empty tar files #91078

progval mannequin opened this issue Mar 4, 2022 · 2 comments · Fixed by #91850
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir

Comments

@progval
Copy link
Mannequin

progval mannequin commented Mar 4, 2022

BPO 46922
Nosy @ethanfurman

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-03-04.16:43:29.909>
labels = ['library', '3.9', '3.10']
title = 'tarfile.TarFile.next() crashes on empty tar files'
updated_at = <Date 2022-03-04.18:05:20.357>
user = 'https://bugs.python.org/progval'

bugs.python.org fields:

activity = <Date 2022-03-04.18:05:20.357>
actor = 'ethan.furman'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2022-03-04.16:43:29.909>
creator = 'progval'
dependencies = []
files = []
hgrepos = []
issue_num = 46922
keywords = []
message_count = 1.0
messages = ['414536']
nosy_count = 2.0
nosy_names = ['progval', 'ethan.furman']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue46922'
versions = ['Python 3.9', 'Python 3.10']

Linked PRs

@progval
Copy link
Mannequin Author

progval mannequin commented Mar 4, 2022

Hi,

I found two related bugs when calling tarfile.TarFile.next() on an empty tar file, both when trying to seek when it should not:

import io
import tarfile

# Create a tarball with no member:
fd = io.BytesIO()
with tarfile.open(fileobj=fd, mode="w") as tf:
    pass

# read in stream mode:
fd.seek(0)
with tarfile.open(fileobj=fd, mode="r|") as tf:
    print(tf.next())   # tarfile.StreamError: seeking backwards is not allowed

# read in normal mode:
fd.seek(0)
with tarfile.open(fileobj=fd, mode="r") as tf:
    print(tf.next())  # ValueError: negative seek value -1

@progval progval mannequin added 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir labels Mar 4, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@dignissimus
Copy link
Contributor

dignissimus commented Apr 23, 2022

It looks like raising ReadError instead might be better.

Edit: After reading the documentation for the next function, merely returning None seems to be the best option here.

iritkatriel added a commit to dignissimus/cpython that referenced this issue Nov 26, 2022
ethanfurman pushed a commit that referenced this issue Nov 26, 2022
…-91850)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 26, 2022
…ty (pythonGH-91850)

(cherry picked from commit 78365b8)

Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
miss-islington added a commit that referenced this issue Nov 26, 2022
…-91850)

(cherry picked from commit 78365b8)

Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant