Commit 789e1c35 authored by Alex A Skinner's avatar Alex A Skinner Committed by Andrew Gerrand

encoding/xml: Do not pass through invalid utf8 bytes

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
parent 51b3611a
...@@ -1758,7 +1758,7 @@ func EscapeText(w io.Writer, s []byte) error { ...@@ -1758,7 +1758,7 @@ func EscapeText(w io.Writer, s []byte) error {
case '\r': case '\r':
esc = esc_cr esc = esc_cr
default: default:
if !isInCharacterRange(r) { if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
esc = esc_fffd esc = esc_fffd
break break
} }
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"unicode/utf8"
) )
const testInput = ` const testInput = `
...@@ -714,3 +715,14 @@ func TestEscapeTextInvalidChar(t *testing.T) { ...@@ -714,3 +715,14 @@ func TestEscapeTextInvalidChar(t *testing.T) {
t.Errorf("have %v, want %v", text, expected) 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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment