diff --git a/internal/language/language.go b/internal/language/language.go index 09d41c73..548358be 100644 --- a/internal/language/language.go +++ b/internal/language/language.go @@ -164,7 +164,12 @@ func (t Tag) MarshalText() (text []byte, err error) { // UnmarshalText implements encoding.TextUnmarshaler. func (t *Tag) UnmarshalText(text []byte) error { - tag, err := Parse(string(text)) + s := string(text) + if strings.Count(s, "-")+strings.Count(s, "_") > 1000 { + *t = Und + return ErrSyntax + } + tag, err := Parse(s) *t = tag return err } diff --git a/internal/language/language_test.go b/internal/language/language_test.go index 269179c2..56c0f18d 100644 --- a/internal/language/language_test.go +++ b/internal/language/language_test.go @@ -6,6 +6,7 @@ package language import ( "reflect" + "strings" "testing" "golang.org/x/text/internal/testtext" @@ -105,6 +106,19 @@ func TestMarshal(t *testing.T) { } } +func TestUnmarshalTextTooBig(t *testing.T) { + for _, separator := range []string{"-", "_"} { + text := []byte("en" + strings.Repeat(separator+"abcdefghi", 1001)) + tag := Tag{LangID: _fr} + if err := tag.UnmarshalText(text); err != ErrSyntax { + t.Errorf("UnmarshalText with %q separators: got error %v; want %v", separator, err, ErrSyntax) + } + if got := tag.String(); got != "und" { + t.Errorf("UnmarshalText with %q separators: got tag %q; want und", separator, got) + } + } +} + func TestParseBase(t *testing.T) { tests := []struct { in string