Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicode unescaping #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ env:
- rel=0.6.4.2
- rel=0.6.5.4

matrix:
allow_failures:
- env: rel=0.6.5.4

go:
- 1.5

Expand Down
69 changes: 53 additions & 16 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,63 @@ func (ds *Dataset) parseLine(line []string) error {
* application.
*/
func unescapeFilepath(path string) (string, error) {
buf := make([]byte, 0, len(path))
llen := len(path)
for i := 0; i < llen; {
if path[i] == '\\' {
if llen < i+4 {
return "", fmt.Errorf("Invalid octal code: too short")
}
octalCode := path[(i + 1):(i + 4)]
val, err := strconv.ParseUint(octalCode, 8, 8)
pattern := "\\0040"
if strings.Index(path, pattern) == -1 {
pattern = "\\040"
if strings.Index(path, pattern) == -1 {
return path, nil
}

}
plen := len(pattern)

orig := make([]byte, len(path))
buf := orig
blen := 0

for {
index := strings.Index(path, pattern)
if index > 0 {
l := copy(buf, path[:index])
path = path[index:]
buf = buf[l:]
blen += l
continue

} else if index == -1 {
l := copy(buf, path)
buf = buf[l:]
blen += l
break

}

path = path[index+plen:]
index = strings.Index(path, pattern)
if index == -1 {
return "", fmt.Errorf("invalid octal code: too short")
}

// skip leading '\' char
escaped := path[1:index]

bytes := strings.Split(escaped, "\\")
blen += len(bytes)

for _, b := range bytes {

val, err := strconv.ParseUint(b, 8, 8)
if err != nil {
return "", fmt.Errorf("Invalid octal code: %v", err)
return "", fmt.Errorf("invalid octal code: %v", err)
}
buf = append(buf, byte(val))
i += 4
} else {
buf = append(buf, path[i])
i++

buf[0] = byte(val)
buf = buf[1:]
}

path = path[index+plen:]
}
return string(buf), nil
return string(orig[:blen]), nil
}

var changeTypeMap = map[string]ChangeType{
Expand Down
40 changes: 40 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package zfs

import (
"errors"
"testing"
)

func TestUnescapeFilePath(t *testing.T) {
var tests = []struct {
escaped string
unescaped string
err error
}{
{"i heart unicode", "i heart unicode", nil},
{"i \\0040\\0342\\0235\\0244 unicode", "", errors.New("invalid octal code: too short")},
{"i \\0040\\0999\\0235\\0244\\0040 unicode", "", errors.New(`invalid octal code: strconv.ParseUint: parsing "0999": invalid syntax`)},
{"i \\0040\\0342\\0235\\0244\\0040 unicode", "i ❤ unicode", nil},
{"i \\0040\\0342\\0235\\0244\\0040\\0040\\0342\\0235\\0244\\0040 unicode", "i ❤❤ unicode", nil},
{"i \\0040\\0342\\0235\\0244\\0040 \\0040\\0342\\0235\\0244\\0040 unicode", "i ❤ ❤ unicode", nil},
{"i \\040\\342\\235\\244\\040 unicode", "i ❤ unicode", nil},
{"i \\040\\342\\235\\244\\040\\040\\342\\235\\244\\040 unicode", "i ❤❤ unicode", nil},
{"i \\040\\342\\235\\244\\040 \\040\\342\\235\\244\\040 unicode", "i ❤ ❤ unicode", nil},
}

for _, test := range tests {
t.Log(test.unescaped)
unescaped, err := unescapeFilepath(test.escaped)
if err != nil && test.err != nil {
if err.Error() != test.err.Error() {
t.Fatalf("1mismatched errors: want:%v, got:%v\n", test.err, err)
}
} else if err != test.err {
t.Fatalf("mismatched errors: want:%v, got:%v\n", test.err, err)
}
if unescaped != test.unescaped {
t.Fatalf("mismatched unescaped:\nwant:|%v|\n got:|%v|\n",
[]byte(test.unescaped), []byte(unescaped))
}
}
}