Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b51cdd3

Browse files
committedAug 10, 2023
add fix and test for non-regular files
1 parent 21ed9e1 commit b51cdd3

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed
 

‎src/archive/tar/writer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package tar
66

77
import (
8+
"errors"
89
"fmt"
910
"io"
1011
"io/fs"
@@ -419,6 +420,10 @@ func (tw *Writer) AddFS(fsys fs.FS) error {
419420
if err != nil {
420421
return err
421422
}
423+
// TODO(#49580): Handle symlinks when fs.ReadLinkFS is available.
424+
if !info.Mode().IsRegular() {
425+
return errors.New("tar: cannot add non-regular file")
426+
}
422427
h, err := FileInfoHeader(info, "")
423428
if err != nil {
424429
return err

‎src/archive/tar/writer_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/hex"
1010
"errors"
1111
"io"
12+
"io/fs"
1213
"os"
1314
"path"
1415
"reflect"
@@ -1335,7 +1336,7 @@ func TestFileWriter(t *testing.T) {
13351336
}
13361337
}
13371338

1338-
func TestWriterAddFs(t *testing.T) {
1339+
func TestWriterAddFS(t *testing.T) {
13391340
fsys := fstest.MapFS{
13401341
"file.go": {Data: []byte("hello")},
13411342
"subfolder/another.go": {Data: []byte("world")},
@@ -1358,8 +1359,7 @@ func TestWriterAddFs(t *testing.T) {
13581359
t.Fatal(err)
13591360
}
13601361

1361-
data := make([]byte, hdr.Size)
1362-
_, err = io.ReadFull(tr, data)
1362+
data, err := io.ReadAll(tr)
13631363
if err != nil {
13641364
t.Fatal(err)
13651365
}
@@ -1375,3 +1375,15 @@ func TestWriterAddFs(t *testing.T) {
13751375
}
13761376
}
13771377
}
1378+
1379+
func TestWriterAddFSNonRegularFiles(t *testing.T) {
1380+
fsys := fstest.MapFS{
1381+
"device": {Data: []byte("hello"), Mode: 0755 | fs.ModeDevice},
1382+
"symlink": {Data: []byte("world"), Mode: 0755 | fs.ModeSymlink},
1383+
}
1384+
var buf bytes.Buffer
1385+
tw := NewWriter(&buf)
1386+
if err := tw.AddFS(fsys); err == nil {
1387+
t.Fatal("expected error, got nil")
1388+
}
1389+
}

0 commit comments

Comments
 (0)
Please sign in to comment.