aboutsummaryrefslogtreecommitdiff
path: root/src/html/template/js.go
diff options
context:
space:
mode:
authorDidier Spezia <didier.06@gmail.com>2015-05-14 22:36:59 +0000
committerRob Pike <r@golang.org>2015-05-19 22:45:50 +0000
commita1c1a763bc7c8d10ec30a7fa60ecf7d5f9a6f1c8 (patch)
tree377cce9b259abe2b715a86a2daea41604a464173 /src/html/template/js.go
parentd6bbcea22a3d4cbcf8350b4b861f0d73ab142ac2 (diff)
downloadgo-a1c1a763bc7c8d10ec30a7fa60ecf7d5f9a6f1c8.tar.xz
html/template: fix string iteration in replacement operations
In css, js, and html, the replacement operations are implemented by iterating on strings (rune by rune). The for/range statement is used. The length of the rune is required and added to the index to properly slice the string. This is potentially wrong because there is a discrepancy between the result of utf8.RuneLen and the increment of the index (set by the for/range statement). For invalid strings, utf8.RuneLen('\ufffd') == 3, while the index is incremented only by 1 byte. htmlReplacer triggers a panic at slicing time for some invalid strings. Use a more robust iteration mechanism based on utf8.DecodeRuneInString, and make sure the same pattern is used for all similar functions in this package. Fixes #10799 Change-Id: Ibad3857b2819435d9fa564f06fc2ca8774102841 Reviewed-on: https://go-review.googlesource.com/10105 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/html/template/js.go')
-rw-r--r--src/html/template/js.go8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/html/template/js.go b/src/html/template/js.go
index 999a61ed07..f6d166b311 100644
--- a/src/html/template/js.go
+++ b/src/html/template/js.go
@@ -246,8 +246,10 @@ func jsRegexpEscaper(args ...interface{}) string {
// `\u2029`.
func replace(s string, replacementTable []string) string {
var b bytes.Buffer
- written := 0
- for i, r := range s {
+ r, w, written := rune(0), 0, 0
+ for i := 0; i < len(s); i += w {
+ // See comment in htmlEscaper.
+ r, w = utf8.DecodeRuneInString(s[i:])
var repl string
switch {
case int(r) < len(replacementTable) && replacementTable[r] != "":
@@ -261,7 +263,7 @@ func replace(s string, replacementTable []string) string {
}
b.WriteString(s[written:i])
b.WriteString(repl)
- written = i + utf8.RuneLen(r)
+ written = i + w
}
if written == 0 {
return s