From 49e6db1c9ed2b2fdbc57fd579f0ca8f6082350be Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Wed, 1 Nov 2017 18:47:04 +0100 Subject: [PATCH] idna: bug fix imported from x/text See CL 73730: avoid memory leak in validation codes Upstream at 8253218a. Change-Id: I3d4860989c8e057f9cc4c9087a78c9c800c5aa7d Reviewed-on: https://go-review.googlesource.com/74954 Reviewed-by: Brad Fitzpatrick --- idna/idna.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/idna/idna.go b/idna/idna.go index 7d328fcb0..346fe4423 100644 --- a/idna/idna.go +++ b/idna/idna.go @@ -309,7 +309,7 @@ func (p *Profile) process(s string, toASCII bool) (string, error) { for ; len(s) > 0 && s[0] == '.'; s = s[1:] { } } - // TODO: allow for a quick check the tables data. + // TODO: allow for a quick check of the tables data. // It seems like we should only create this error on ToASCII, but the // UTS 46 conformance tests suggests we should always check this. if err == nil && p.verifyDNSLength && s == "" { @@ -405,6 +405,9 @@ func validateRegistration(p *Profile, s string) (idem string, bidi bool, err err } for i := 0; i < len(s); { v, sz := trie.lookupString(s[i:]) + if sz == 0 { + return s, bidi, runeError(utf8.RuneError) + } bidi = bidi || info(v).isBidi(s[i:]) // Copy bytes not copied so far. switch p.simplify(info(v).category()) { @@ -446,6 +449,15 @@ func validateAndMap(p *Profile, s string) (vm string, bidi bool, err error) { var combinedInfoBits info for i := 0; i < len(s); { v, sz := trie.lookupString(s[i:]) + if sz == 0 { + b = append(b, s[k:i]...) + b = append(b, "\ufffd"...) + k = len(s) + if err == nil { + err = runeError(utf8.RuneError) + } + break + } combinedInfoBits |= info(v) bidi = bidi || info(v).isBidi(s[i:]) start := i @@ -584,6 +596,9 @@ func validateFromPunycode(p *Profile, s string) error { // loop. for i := 0; i < len(s); { v, sz := trie.lookupString(s[i:]) + if sz == 0 { + return runeError(utf8.RuneError) + } if c := p.simplify(info(v).category()); c != valid && c != deviation { return &labelError{s, "V6"} }