1
0
mirror of https://github.com/golang/go synced 2024-11-17 18:14:46 -07:00

net: don't crash on Windows when Lookup name has null byte in string

Fixes #31597

Change-Id: I0db1f6f457632c49f9ecfa9d85b99b4cf7d91325
Reviewed-on: https://go-review.googlesource.com/c/go/+/173362
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
Brad Fitzpatrick 2019-04-23 14:57:09 +00:00
parent 17615969b6
commit 2e11881269
2 changed files with 15 additions and 1 deletions

View File

@ -1184,3 +1184,13 @@ func TestWithUnexpiredValuesPreserved(t *testing.T) {
t.Errorf("Lookup after expiry: Got %v want nil", g) t.Errorf("Lookup after expiry: Got %v want nil", g)
} }
} }
// Issue 31586: don't crash on null byte in name
func TestLookupNullByte(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.SkipFlakyNet(t)
_, err := LookupHost("foo\x00bar") // used to crash on Windows
if err == nil {
t.Errorf("unexpected success")
}
}

View File

@ -101,7 +101,11 @@ func (r *Resolver) lookupIP(ctx context.Context, network, name string) ([]IPAddr
Protocol: syscall.IPPROTO_IP, Protocol: syscall.IPPROTO_IP,
} }
var result *syscall.AddrinfoW var result *syscall.AddrinfoW
e := syscall.GetAddrInfoW(syscall.StringToUTF16Ptr(name), nil, &hints, &result) name16p, err := syscall.UTF16PtrFromString(name)
if err != nil {
return nil, &DNSError{Name: name, Err: err.Error()}
}
e := syscall.GetAddrInfoW(name16p, nil, &hints, &result)
if e != nil { if e != nil {
err := winError("getaddrinfow", e) err := winError("getaddrinfow", e)
dnsError := &DNSError{Err: err.Error(), Name: name} dnsError := &DNSError{Err: err.Error(), Name: name}