Skip to content

Commit efb744f

Browse files
TimothyGuneild
authored andcommitted
internal/export/idna: fix infinite loop in Go pre-1.10
lookupString returns 0 for incomplete UTF-8 sequences, so to prevent infinite loops we must specifically check for that case. However, CL 73730 which fixed this issue in 2017 was lost in the shuffle that allowed multiple Unicode versions in x/text (CL 83235), and the fix was never applied to idna9.0.0.go. This CL fixes that oversight. Updates golang/go#22184 Change-Id: I3a6ab08b157f4017560020ff259d1afbe49a9e71 Reviewed-on: https://go-review.googlesource.com/c/text/+/361494 Reviewed-by: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 48e4a4a commit efb744f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

internal/export/idna/idna9.0.0.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ type options struct {
197197
bidirule func(s string) bool
198198
}
199199

200-
// A Profile defines the configuration of a IDNA mapper.
200+
// A Profile defines the configuration of an IDNA mapper.
201201
type Profile struct {
202202
options
203203
}
@@ -426,6 +426,9 @@ func validateRegistration(p *Profile, s string) (string, error) {
426426
}
427427
for i := 0; i < len(s); {
428428
v, sz := trie.lookupString(s[i:])
429+
if sz == 0 {
430+
return s, runeError(utf8.RuneError)
431+
}
429432
// Copy bytes not copied so far.
430433
switch p.simplify(info(v).category()) {
431434
// TODO: handle the NV8 defined in the Unicode idna data set to allow
@@ -448,6 +451,15 @@ func validateAndMap(p *Profile, s string) (string, error) {
448451
)
449452
for i := 0; i < len(s); {
450453
v, sz := trie.lookupString(s[i:])
454+
if sz == 0 {
455+
b = append(b, s[k:i]...)
456+
b = append(b, "\ufffd"...)
457+
k = len(s)
458+
if err == nil {
459+
err = runeError(utf8.RuneError)
460+
}
461+
break
462+
}
451463
start := i
452464
i += sz
453465
// Copy bytes not copied so far.
@@ -580,6 +592,9 @@ func validateFromPunycode(p *Profile, s string) error {
580592
}
581593
for i := 0; i < len(s); {
582594
v, sz := trie.lookupString(s[i:])
595+
if sz == 0 {
596+
return runeError(utf8.RuneError)
597+
}
583598
if c := p.simplify(info(v).category()); c != valid && c != deviation {
584599
return &labelError{s, "V6"}
585600
}

internal/export/idna/idna9.0.0_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func TestLabelErrors(t *testing.T) {
7878
{resolve, "\u3002b", "b", ""},
7979
{resolve, "..b", "b", ""},
8080
{resolve, "b..", "b..", ""},
81+
{resolve, "\xed", "", "P1"},
8182

8283
// Raw punycode
8384
{punyA, "", "", ""},

0 commit comments

Comments
 (0)