Skip to content

strings: Map does not process invalid UTF-8 sequences correctly #19330

@martisch

Description

@martisch

go tip

The new strings.Map implementation introduced in http://golang.org/cl/33201/ does not correctly handle strings with invalid UTF-8 sequences.
This is due to a lack of differentiating between decoding an invalid sequence and the RuneError rune.

https://play.golang.org/p/A-UxFJNWAb

	replaceNotLatin := func(r rune) rune {
		if unicode.Is(unicode.Latin, r) {
			return r
		}
		return '?'
	}
	fmt.Println(strings.Map(replaceNotLatin, "Hello\255World"))

should print "Hello?World" but prints "Hello?rld".

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions