8000 Fix panics when attempting to get file attributes from too small byte… · etherscan-io/sftp@1bb2f28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bb2f28

Browse files
committed
Fix panics when attempting to get file attributes from too small byte slice
1 parent 8488d36 commit 1bb2f28

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

attrs.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,30 @@ func unmarshalAttrs(b []byte) (*FileStat, []byte) {
102102
func getFileStat(flags uint32, b []byte) (*FileStat, []byte) {
103103
var fs FileStat
104104
if flags&sshFileXferAttrSize == sshFileXferAttrSize {
105-
fs.Size, b = unmarshalUint64(b)
105+
fs.Size, b, _ = unmarshalUint64Safe(b)
106106
}
107107
if flags&sshFileXferAttrUIDGID == sshFileXferAttrUIDGID {
108-
fs.UID, b = unmarshalUint32(b)
108+
fs.UID, b, _ = unmarshalUint32Safe(b)
109109
}
110110
if flags&sshFileXferAttrUIDGID == sshFileXferAttrUIDGID {
111-
fs.GID, b = unmarshalUint32(b)
111+
fs.GID, b, _ = unmarshalUint32Safe(b)
112112
}
113113
if flags&sshFileXferAttrPermissions == sshFileXferAttrPermissions {
114-
fs.Mode, b = unmarshalUint32(b)
114+
fs.Mode, b, _ = unmarshalUint32Safe(b)
115115
}
116116
if flags&sshFileXferAttrACmodTime == sshFileXferAttrACmodTime {
117-
fs.Atime, b = unmarshalUint32(b)
118-
fs.Mtime, b = unmarshalUint32(b)
117+
fs.Atime, b, _ = unmarshalUint32Safe(b)
118+
fs.Mtime, b, _ = unmarshalUint32Safe(b)
119119
}
120120
if flags&sshFileXferAttrExtented == sshFileXferAttrExtented {
121121
var count uint32
122-
count, b = unmarshalUint32(b)
122+
count, b, _ = unmarshalUint32Safe(b)
123123
ext := make([]StatExtended, count)
124124
for i := uint32(0); i < count; i++ {
125125
var typ string
126126
var data string
127-
typ, b = unmarshalString(b)
128-
data, b = unmarshalString(b)
127+
typ, b, _ = unmarshalStringSafe(b)
128+
data, b, _ = unmarsha 8000 lStringSafe(b)
129129
ext[i] = StatExtended{typ, data}
130130
}
131131
fs.Extended = ext

request-attrs_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sftp
22

33
import (
4+
"math"
45
"os"
56

67
"github.com/stretchr/testify/assert"
@@ -49,3 +50,11 @@ func TestRequestAttributes(t *testing.T) {
4950
assert.False(t, testFs.FileMode().IsDir())
5051
assert.Equal(t, testFs.FileMode().Perm(), os.FileMode(700).Perm())
5152
}
53+
54+
func TestRequestAttributesEmpty(t *testing.T) {
55+
fs, b := getFileStat(math.MaxUint32, nil)
56+
assert.Equal(t, &FileStat{
57+
Extended: []StatExtended{},
58+
}, fs)
59+
assert.Empty(t, b)
60+
}

0 commit comments

Comments
 (0)
0