wiki: fix crash with blob name contains tab (#3916)

This commit is contained in:
Unknwon
2017-02-16 11:47:54 -05:00
parent f129e0ecb5
commit 3b49a99b60
6 changed files with 20 additions and 22 deletions

View File

@@ -29,25 +29,23 @@ func NewTree(repo *Repository, id sha1) *Tree {
}
}
var escapeChar = []byte("\\")
// Predefine []byte variables to avoid runtime allocations.
var (
escapedSlash = []byte(`\\`)
regularSlash = []byte(`\`)
escapedTab = []byte(`\t`)
regularTab = []byte("\t")
)
// UnescapeChars reverses escaped characters.
func UnescapeChars(in []byte) []byte {
if bytes.Index(in, escapeChar) == -1 {
// LEGACY [Go 1.7]: use more expressive bytes.ContainsAny
if bytes.IndexAny(in, "\\\t") == -1 {
return in
}
endIdx := len(in) - 1
isEscape := false
out := make([]byte, 0, endIdx+1)
for i := range in {
if in[i] == '\\' && !isEscape {
isEscape = true
continue
}
isEscape = false
out = append(out, in[i])
}
out := bytes.Replace(in, escapedSlash, regularSlash, -1)
out = bytes.Replace(out, escapedTab, regularTab, -1)
return out
}