internal/export/idna: fix an infinite loop in CheckJoiners

Occurs only when using a profile with CheckJoiners(true),
but no mapping or label validation (highly unusual, not likely
to occur in the real world).

Fixes golang/go#82864

Change-Id: I67c9ce5072c4e370fb802276925656c86a6a6964
Reviewed-on: https://go-review.googlesource.com/c/text/+/859461
Reviewed-by: Nicholas Husin <nsh@golang.org>
Reviewed-by: Nicholas Husin <husin@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/internal/export/idna/idna.go b/internal/export/idna/idna.go
index cb938a2..18d6e01 100644
--- a/internal/export/idna/idna.go
+++ b/internal/export/idna/idna.go
@@ -840,6 +840,9 @@
 			break
 		}
 		v, sz = trie.lookupString(s[i:])
+		if sz == 0 {
+			return runeError{utf8.RuneError, "P1"}
+		}
 		x = info(v)
 	}
 	if st == stateFAIL || st == stateAfter {
diff --git a/internal/export/idna/idna_test.go b/internal/export/idna/idna_test.go
index 1646ae7..32b04a0 100644
--- a/internal/export/idna/idna_test.go
+++ b/internal/export/idna/idna_test.go
@@ -174,6 +174,8 @@
 	transitional := kind{"Transitional", p.ToASCII}
 	p = New(MapForLookup(), Transitional(false))
 	nontransitional := kind{"Nontransitional", p.ToASCII}
+	p = New(CheckJoiners(true))
+	joiners := kind{"CheckJoiners", p.ToASCII}
 
 	testCases := []struct {
 		kind
@@ -283,6 +285,11 @@
 		// to look up the input punycode.
 		{resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"},
 		{display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"},
+
+		// CheckJoiners
+		{joiners, "\u200d\xe2\x80", "", "P1"},
+		{joiners, "\u200c\xe2\x80", "", "P1"},
+		{joiners, "\u200d\xed", "", "P1"},
 	}
 
 	for _, tc := range testCases {