Skip to content

Commit 539a6f1

Browse files
authored
Fix index out of range (#26)
If the string only contains a backslash.
1 parent 5bb5ecf commit 539a6f1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

unserialize.go

+22-18
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,28 @@ func DecodePHPString(data []byte) string {
2626
var buffer bytes.Buffer
2727
for i := 0; i < len(data); i++ {
2828
if data[i] == '\\' {
29-
switch data[i+1] {
30-
case 'x':
31-
b, _ := strconv.ParseInt(string(data[i+2:i+4]), 16, 32)
32-
buffer.WriteByte(byte(b))
33-
i += 3
34-
35-
case 'n':
36-
buffer.WriteByte('\n')
37-
i++
38-
39-
case '\'':
40-
buffer.WriteByte(data[i+1])
41-
i++
42-
43-
default:
44-
// It's a bit annoying but a backlash itself is not escaped. So
45-
// if it was not followed by a known character we have to assume
46-
// this.
29+
if i+1 <= len(data)-1 {
30+
switch data[i+1] {
31+
case 'x':
32+
b, _ := strconv.ParseInt(string(data[i+2:i+4]), 16, 32)
33+
buffer.WriteByte(byte(b))
34+
i += 3
35+
36+
case 'n':
37+
buffer.WriteByte('\n')
38+
i++
39+
40+
case '\'':
41+
buffer.WriteByte(data[i+1])
42+
i++
43+
44+
default:
45+
// It's a bit annoying but a backlash itself is not escaped. So
46+
// if it was not followed by a known character we have to assume
47+
// this.
48+
buffer.WriteByte('\\')
49+
}
50+
} else {
4751
buffer.WriteByte('\\')
4852
}
4953
} else {

unserialize_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func TestUnmarshalString(t *testing.T) {
233233
nil,
234234
},
235235
"not a string": {[]byte("N;"), "", errors.New("not a string")},
236+
"Backslash": {[]byte("s:1:\"\\\";"), "\\", nil},
236237
}
237238

238239
for testName, test := range tests {

0 commit comments

Comments
 (0)