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

archive/tar: should treat empty tar files as invalid #31012

Closed
tomfitzhenry opened this issue Mar 23, 2019 · 3 comments
Closed

archive/tar: should treat empty tar files as invalid #31012

tomfitzhenry opened this issue Mar 23, 2019 · 3 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@tomfitzhenry
Copy link

What version of Go are you using (go version)?

https://play.golang.org/

What did you do?

https://play.golang.org/p/VeyYROaJQZu

What did you expect to see?

An error to be printed, similar to how GNU tar produces an error when reading an empty file:

$ touch empty
$ tar --version
tar (GNU tar) 1.29
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by John Gilmore and Jay Fenlason.
$ tar tf empty
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors

What did you see instead?

No error printed.

@tomfitzhenry tomfitzhenry changed the title An empty file is treated as a valid tar file archive/tar treats empty tar files as valid Mar 23, 2019
@dsnet dsnet changed the title archive/tar treats empty tar files as valid archive/tar: should treat empty tar files as invalid Mar 23, 2019
@dsnet dsnet added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 23, 2019
@dsnet
Copy link
Member

dsnet commented Mar 23, 2019

tar archives lack a unified formal specification of what the format actually is. Though the USTAR and PAX formats specify that an archive should end with 2 blocks of zeros, the reality is that this specification came after the world has been doing all sorts of weird things. Most parsers treat the last 2 blocks of zeros as optional for maximal compatibility. This even goes for GNU tar, which also treats those blocks as optional. It just so happens that GNU tar has a special case where they fail on an empty file. On the other hand, BSD tar seems perfectly happy parsing an empty file.

Personally, I think we should keep the current behavior since an empty file should be valid supposing the trailing 2 blocks of zeros is considered optional. Also, turning an empty file into an error now is likely to break people who have come to depend on this behavior.

@cuonglm
Copy link
Member

cuonglm commented Mar 23, 2019

For other references, perl also treat empty file as valid:

perl -MArchive::Tar -e 'my $tar = Archive::Tar->new; $tar->read("./empty")'

python raise error:

python -c 'import tarfile; tarfile.TarFile("./empty")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/tarfile.py", line 1587, in __init__
    self.firstmember = self.next()
  File "/usr/lib/python2.7/tarfile.py", line 2371, in next
    raise ReadError("empty file")
tarfile.ReadError: empty file

@dsnet
Copy link
Member

dsnet commented Aug 5, 2019

I'm going to close this as working as intended:

  1. Had we been implementing archive/tar from scratch perhaps we could have treated an empty file as invalid, but that has not be the case since Go 1.0. Changing it may break people relying on this property.
  2. Many tar implementations also treat empty file as valid, so we are hardly the exception.

@dsnet dsnet closed this as completed Aug 5, 2019
@golang golang locked and limited conversation to collaborators Aug 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants