]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: Do not pass through invalid utf8 bytes
authorAlex A Skinner <alex@lx.lc>
Tue, 30 Jul 2013 04:11:47 +0000 (14:11 +1000)
committerAndrew Gerrand <adg@golang.org>
Tue, 30 Jul 2013 04:11:47 +0000 (14:11 +1000)
EscapeText now escapes 0xFFFD returned from DecodeRune as 0xFFFD, rather than passing through the original byte.
Fixes #5880.

R=golang-dev, r, bradfitz, adg
CC=golang-dev
https://golang.org/cl/11975043

src/pkg/encoding/xml/xml.go
src/pkg/encoding/xml/xml_test.go

index 021f7e47d91d31ca9200c0b8ae04a9c2a7330b55..2f36604797df01a7257bbfc6e41b1d592f3ea8f4 100644 (file)
@@ -1758,7 +1758,7 @@ func EscapeText(w io.Writer, s []byte) error {
                case '\r':
                        esc = esc_cr
                default:
-                       if !isInCharacterRange(r) {
+                       if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
                                esc = esc_fffd
                                break
                        }
index eeedbe575f8b76fb99489d8134c46eb5d380c0f5..52477d342947395d5a1f1485c490c4775d3ff606 100644 (file)
@@ -11,6 +11,7 @@ import (
        "reflect"
        "strings"
        "testing"
+       "unicode/utf8"
 )
 
 const testInput = `
@@ -714,3 +715,14 @@ func TestEscapeTextInvalidChar(t *testing.T) {
                t.Errorf("have %v, want %v", text, expected)
        }
 }
+
+func TestIssue5880(t *testing.T) {
+       type T []byte
+       data, err := Marshal(T{192, 168, 0, 1})
+       if err != nil {
+               t.Errorf("Marshal error: %v", err)
+       }
+       if !utf8.Valid(data) {
+               t.Errorf("Marshal generated invalid UTF-8: %x", data)
+       }
+}