Skip to content

Commit

Permalink
message/pipeline: detect unknown keys
Browse files Browse the repository at this point in the history
When (d *dictionary) Lookup() is called on an non-existing key, the
`messageKeyToIndex[key]` returns 0. As 0 is also a valid entry in
messageKeyToIndex, the method returned the wrong value rather than fail.

Fixes golang/go#35587

Change-Id: Iedd1cf42f29335c2c2052b07993d7f2dfcd3cc6c
Reviewed-on: https://go-review.googlesource.com/c/text/+/207217
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
rozmansi authored and mpvl committed Dec 30, 2019
1 parent cbf43d2 commit 929e72c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/gotext/examples/extract/catalog.go

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

5 changes: 4 additions & 1 deletion cmd/gotext/examples/extract_http/catalog_gen.go

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

5 changes: 4 additions & 1 deletion message/pipeline/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ type dictionary struct {
}
func (d *dictionary) Lookup(key string) (data string, ok bool) {
p := messageKeyToIndex[key]
p, ok := messageKeyToIndex[key]
if !ok {
return "", false
}
start, end := d.index[p], d.index[p+1]
if start == end {
return "", false
Expand Down
5 changes: 4 additions & 1 deletion message/pipeline/testdata/ssa/catalog_gen.go

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

5 changes: 4 additions & 1 deletion message/pipeline/testdata/test1/catalog_gen.go.want
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type dictionary struct {
}

func (d *dictionary) Lookup(key string) (data string, ok bool) {
p := messageKeyToIndex[key]
p, ok := messageKeyToIndex[key]
if !ok {
return "", false
}
start, end := d.index[p], d.index[p+1]
if start == end {
return "", false
Expand Down
4 changes: 4 additions & 0 deletions message/pipeline/testdata/test1/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func TestCatalog(t *testing.T) {
lang: "en",
key: "Hello world!\n",
want: "Hello world!\n",
}, {
lang: "en",
key: "non-existing-key\n",
want: "non-existing-key\n",
}, {
lang: "de",
key: "Hello world!\n",
Expand Down

0 comments on commit 929e72c

Please sign in to comment.