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

x/text/encoding: UTF-16 decoder handles unpaired surrogates incorrectly #39492

Open
abacabadabacaba opened this issue Jun 9, 2020 · 1 comment
Open

Comments

@abacabadabacaba
Copy link

@abacabadabacaba abacabadabacaba commented Jun 9, 2020

When decoding some strings containing unpaired surrogates, UTF-16 decoder produces wrong number of \ufffd runes. Some examples:

  • On string \xdc\x00\xdc\x00: expected result \ufffd\ufffd (two copies of \ufffd), actual result \ufffd.
  • On string \xd8\x00\x00: expected result \ufffd, actual result \ufffd\ufffd.

The expected results are derived from a WhatWG spec.

Also, the name of internal function isHighSurrogate is misleading: it actually checks whether the argument is a low surrogate.

Code to reproduce:

package main

import (
	"golang.org/x/text/encoding/unicode"
	"fmt"
)

func main() {
	res, err := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder().String("\xd8\x00 ")
	fmt.Println(res, err)
}
@gopherbot gopherbot added this to the Unreleased milestone Jun 9, 2020
@odeke-em
Copy link
Member

@odeke-em odeke-em commented Jun 10, 2020

Thank you for this report @abacabadabacaba!

Kindly cc-ing @mpvl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.