1
0
mirror of https://github.com/golang/go synced 2024-11-23 10:40:08 -07:00

net: fix LookupTXT of long records on Windows

The response to a TXT lookup is a sequence of RRs,
each of which contains a sequence of string fragments.

The correct handling of the response is to do:

    for each rr {
        list = append(list, strings.Join(rr.fragments, ""))
    }

(like in at dnsRR_TXT.Walk, used on most platforms).

The Windows code incorrectly does:

    for each rr {
        list = append(list, rr.fragments...)
    }

This CL fixes it to concatenate fragments, as it must.

Fixes #21472.

Change-Id: I78cce96f172e5e90da9a212b0343457f6d5f92e8
Reviewed-on: https://go-review.googlesource.com/79555
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Russ Cox 2017-11-22 16:15:11 -05:00
parent 571ee0436f
commit b6cf58d5b8
2 changed files with 24 additions and 2 deletions

View File

@ -9,7 +9,9 @@ import (
"context" "context"
"fmt" "fmt"
"internal/testenv" "internal/testenv"
"reflect"
"runtime" "runtime"
"sort"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -303,6 +305,25 @@ func TestLookupGoogleHost(t *testing.T) {
} }
} }
func TestLookupLongTXT(t *testing.T) {
if testenv.Builder() == "" {
testenv.MustHaveExternalNetwork(t)
}
txts, err := LookupTXT("golang.rsc.io")
if err != nil {
t.Fatal(err)
}
sort.Strings(txts)
want := []string{
strings.Repeat("abcdefghijklmnopqrstuvwxyABCDEFGHJIKLMNOPQRSTUVWXY", 10),
"gophers rule",
}
if !reflect.DeepEqual(txts, want) {
t.Fatalf("LookupTXT golang.rsc.io incorrect\nhave %q\nwant %q", txts, want)
}
}
var lookupGoogleIPTests = []struct { var lookupGoogleIPTests = []struct {
name string name string
}{ }{

View File

@ -279,10 +279,11 @@ func (*Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
txts := make([]string, 0, 10) txts := make([]string, 0, 10)
for _, p := range validRecs(r, syscall.DNS_TYPE_TEXT, name) { for _, p := range validRecs(r, syscall.DNS_TYPE_TEXT, name) {
d := (*syscall.DNSTXTData)(unsafe.Pointer(&p.Data[0])) d := (*syscall.DNSTXTData)(unsafe.Pointer(&p.Data[0]))
s := ""
for _, v := range (*[1 << 10]*uint16)(unsafe.Pointer(&(d.StringArray[0])))[:d.StringCount] { for _, v := range (*[1 << 10]*uint16)(unsafe.Pointer(&(d.StringArray[0])))[:d.StringCount] {
s := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(v))[:]) s += syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(v))[:])
txts = append(txts, s)
} }
txts = append(txts, s)
} }
return txts, nil return txts, nil
} }