Skip to content

Commit

Permalink
Fix index out of range (#26)
Browse files Browse the repository at this point in the history
If the string only contains a backslash.
  • Loading branch information
yxing-xyz authored Dec 4, 2021
1 parent 5bb5ecf commit 539a6f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
40 changes: 22 additions & 18 deletions unserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ func DecodePHPString(data []byte) string {
var buffer bytes.Buffer
for i := 0; i < len(data); i++ {
if data[i] == '\\' {
switch data[i+1] {
case 'x':
b, _ := strconv.ParseInt(string(data[i+2:i+4]), 16, 32)
buffer.WriteByte(byte(b))
i += 3

case 'n':
buffer.WriteByte('\n')
i++

case '\'':
buffer.WriteByte(data[i+1])
i++

default:
// It's a bit annoying but a backlash itself is not escaped. So
// if it was not followed by a known character we have to assume
// this.
if i+1 <= len(data)-1 {
switch data[i+1] {
case 'x':
b, _ := strconv.ParseInt(string(data[i+2:i+4]), 16, 32)
buffer.WriteByte(byte(b))
i += 3

case 'n':
buffer.WriteByte('\n')
i++

case '\'':
buffer.WriteByte(data[i+1])
i++

default:
// It's a bit annoying but a backlash itself is not escaped. So
// if it was not followed by a known character we have to assume
// this.
buffer.WriteByte('\\')
}
} else {
buffer.WriteByte('\\')
}
} else {
Expand Down
1 change: 1 addition & 0 deletions unserialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func TestUnmarshalString(t *testing.T) {
nil,
},
"not a string": {[]byte("N;"), "", errors.New("not a string")},
"Backslash": {[]byte("s:1:\"\\\";"), "\\", nil},
}

for testName, test := range tests {
Expand Down

0 comments on commit 539a6f1

Please sign in to comment.