8000 Merge pull request #328 from vansante/fix-empty-attributes-panicking · etherscan-io/sftp@939a693 · GitHub
[go: up one dir, main page]

Skip to content

Commit 939a693

Browse files
authored
Merge pull request pkg#328 from vansante/fix-empty-attributes-panicking
Fix panics when attempting to get file attributes from too small byte slices
2 parents 8488d36 + f94839f commit 939a693

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

attrs.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const (
1515
sshFileXferAttrPermissions = 0x00000004
1616
sshFileXferAttrACmodTime = 0x00000008
1717
sshFileXferAttrExtented = 0x80000000
18+
19+
sshFileXferAttrAll = sshFileXferAttrSize|sshFileXferAttrUIDGID|sshFileXferAttrPermissions|
20+
sshFileXferAttrACmodTime|sshFileXferAttrExtented
1821
)
1922

2023
// fileInfo is an artificial type designed to satisfy os.FileInfo.
@@ -102,30 +105,30 @@ func unmarshalAttrs(b []byte) (*FileStat, []byte) {
102105
func getFileStat(flags uint32, b []byte) (*FileStat, []byte) {
103106
var fs FileStat
104107
if flags&sshFileXferAttrSize == sshFileXferAttrSize {
105-
fs.Size, b = unmarshalUint64(b)
108+
fs.Size, b, _ = unmarshalUint64Safe(b)
106109
}
107110
if flags&sshFileXferAttrUIDGID == sshFileXferAttrUIDGID {
108-
fs.UID, b = unmarshalUint32(b)
111+
fs.UID, b, _ = unmarshalUint32Safe(b)
109112
}
110113
if flags&sshFileXferAttrUIDGID == sshFileXferAttrUIDGID {
111-
fs.GID, b = unmarshalUint32(b)
114+
fs.GID, b, _ = unmarshalUint32Safe(b)
112115
}
113116
if flags&sshFileXferAttrPermissions == sshFileXferAttrPermissions {
114-
fs.Mode, b = unmarshalUint32(b)
117+
fs.Mode, b, _ = unmarshalUint32Safe(b)
115118
}
116119
if flags&sshFileXferAttrACmodTime == sshFileXferAttrACmodTime {
117-
fs.Atime, b = unmarshalUint32(b)
118-
fs.Mtime, b = unmarshalUint32(b)
120+
fs.Atime, b, _ = unmarshalUint32Safe(b)
121+
fs.Mtime, b, _ = unmarshalUint32Safe(b)
119122
}
120123
if flags&sshFileXferAttrExtented == sshFileXferAttrExtented {
121124
var count uint32
122-
count, b = unmarshalUint32(b)
125+
count, b, _ = unmarshalUint32Safe(b)
123126
ext := make([]StatExtended, count)
124127
for i := uint32(0); i < count; i++ {
125128
var typ string
126129
var data string
127-
typ, b = unmarshalString(b)
128-
data, b = unmarshalString(b)
130+
typ, b, _ = unmarshalStringSafe(b)
131+
data, b, _ = unmarshalStringSafe(b)
129132
ext[i] = StatExtended{typ, data}
130133
}
131134
fs.Extended = ext

request-attrs_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package sftp
22

33
import (
44
"os"
5+
"testing"
56

67
"github.com/stretchr/testify/assert"
7-
8-
"testing"
98
)
109

1110
func TestRequestPflags(t *testing.T) {
@@ -49,3 +48,11 @@ func TestRequestAttributes(t *testing.T) {
4948
assert.False(t, testFs.FileMode().IsDir())
5049
assert.Equal(t, testFs.FileMode().Perm(), os.FileMode(700).Perm())
5150
}
51+
52+
func TestRequestAttributesEmpty(t *testing.T) {
53+
fs, b := getFileStat(sshFileXferAttrAll, nil)
54+
assert.Equal(t, &FileStat{
55+
Extended: []StatExtended{},
56+
}, fs)
57+
assert.Empty(t, b)
58+
}

0 commit comments

Comments
 (0)
0