Skip to content

Commit

Permalink
language: fix off-by-one error
Browse files Browse the repository at this point in the history
Regions are encoded starting from 1. However, one of the
region-related tables assumed 0-based indices. This
caused a crash when used with ZZ, the largest region.

Fixes golang/go#43834

Change-Id: Iaed6b9d2683cd50504e6d33c8a6df8b21dd1687d
Reviewed-on: https://go-review.googlesource.com/c/text/+/305469
Trust: Marcel van Lohuizen <mpvl@golang.org>
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Seth Vargo <sethvargo@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
mpvl authored and xhit committed Oct 10, 2022
1 parent 3b780af commit 0154e0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion language/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func (b *builder) writeMatchData() {
regions := strings.Split(g.Contains, " ")
regionHierarchy[g.Type] = append(regionHierarchy[g.Type], regions...)
}
regionToGroups := make([]uint8, language.NumRegions)
// Regions start at 1, so the slice must be one larger than the number of
// regions.
regionToGroups := make([]uint8, language.NumRegions+1)

idToIndex := map[string]uint8{}
for i, mv := range lm[0].MatchVariable {
Expand Down
14 changes: 14 additions & 0 deletions language/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ func (t haveTag) String() string {
return fmt.Sprintf("%v:%d:%v:%v-%v|%v", t.tag, t.index, t.conf, t.maxRegion, t.maxScript, t.altScript)
}

func TestIssue43834(t *testing.T) {
matcher := NewMatcher([]Tag{English})

// ZZ is the largest region code and should not cause overflow.
desired, _, err := ParseAcceptLanguage("en-ZZ")
if err != nil {
t.Error(err)
}
_, i, _ := matcher.Match(desired...)
if i != 0 {
t.Errorf("got %v; want 0", i)
}
}

func TestBestMatchAlloc(t *testing.T) {
m := NewMatcher(makeTagList("en sr nl"))
// Go allocates when creating a list of tags from a single tag!
Expand Down
8 changes: 4 additions & 4 deletions language/tables.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0154e0b

Please sign in to comment.