message/pipeline: detect unknown keys 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>
diff --git a/cmd/gotext/examples/extract/catalog.go b/cmd/gotext/examples/extract/catalog.go index bc6130a..65570f8 100644 --- a/cmd/gotext/examples/extract/catalog.go +++ b/cmd/gotext/examples/extract/catalog.go
@@ -14,7 +14,10 @@ } 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
diff --git a/cmd/gotext/examples/extract_http/catalog_gen.go b/cmd/gotext/examples/extract_http/catalog_gen.go index 2c410dc..4327d34 100644 --- a/cmd/gotext/examples/extract_http/catalog_gen.go +++ b/cmd/gotext/examples/extract_http/catalog_gen.go
@@ -14,7 +14,10 @@ } 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
diff --git a/message/pipeline/generate.go b/message/pipeline/generate.go index 2770d14..bb1f85b 100644 --- a/message/pipeline/generate.go +++ b/message/pipeline/generate.go
@@ -297,7 +297,10 @@ } 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
diff --git a/message/pipeline/testdata/ssa/catalog_gen.go b/message/pipeline/testdata/ssa/catalog_gen.go index 539d741..dce54af 100644 --- a/message/pipeline/testdata/ssa/catalog_gen.go +++ b/message/pipeline/testdata/ssa/catalog_gen.go
@@ -14,7 +14,10 @@ } 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
diff --git a/message/pipeline/testdata/test1/catalog_gen.go.want b/message/pipeline/testdata/test1/catalog_gen.go.want index 7d93f48..890ef51 100644 --- a/message/pipeline/testdata/test1/catalog_gen.go.want +++ b/message/pipeline/testdata/test1/catalog_gen.go.want
@@ -14,7 +14,10 @@ } 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
diff --git a/message/pipeline/testdata/test1/catalog_test.go b/message/pipeline/testdata/test1/catalog_test.go index eeb7c25..34f8b86 100644 --- a/message/pipeline/testdata/test1/catalog_test.go +++ b/message/pipeline/testdata/test1/catalog_test.go
@@ -23,6 +23,10 @@ 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", want: "Hallo Welt!\n",