diff options
author | Mateusz Poliwczak <mpoliwczak34@gmail.com> | 2022-12-01 17:54:49 +0000 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-12-01 21:00:45 +0000 |
commit | 93587d3591f6cb5cd21951a03c3aa750873f2601 (patch) | |
tree | 9e5aa5d200d9c40f2e453b2a8a5afbbd74ed5296 | |
parent | 7e92b80c21ea22b9dcfb27cc68c61b805485f379 (diff) | |
download | go-93587d3591f6cb5cd21951a03c3aa750873f2601.tar.gz go-93587d3591f6cb5cd21951a03c3aa750873f2601.zip |
net: retry with bigger buffer in resSearch
Glibc returns size > bufSize, when the entire dns reply does not fit inside the provided buffer.
Change-Id: Ie1c1c6a3411880bd8bdb4371f1f1b7bcce837ea2
GitHub-Last-Rev: 488cd3ed0db2a86433aa921117b8f1e9192b1fa5
GitHub-Pull-Request: golang/go#57020
Reviewed-on: https://go-review.googlesource.com/c/go/+/454395
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
-rw-r--r-- | src/net/cgo_unix.go | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/net/cgo_unix.go b/src/net/cgo_unix.go index b9759d05a3..ef003b78bd 100644 --- a/src/net/cgo_unix.go +++ b/src/net/cgo_unix.go @@ -346,17 +346,30 @@ func resSearch(ctx context.Context, hostname string, rtype, class int) ([]dnsmes // giving us no way to find out how big the packet is. // For now, we are willing to take res_search's word that there's nothing // useful in the response, even though there *is* a response. - const bufSize = 1500 - buf := (*_C_uchar)(_C_malloc(bufSize)) + bufSize := maxDNSPacketSize + buf := (*_C_uchar)(_C_malloc(uintptr(bufSize))) defer _C_free(unsafe.Pointer(buf)) + s := _C_CString(hostname) defer _C_FreeCString(s) - size, err := _C_res_nsearch(state, s, class, rtype, buf, bufSize) - if size <= 0 || size > bufSize { - return nil, errors.New("res_nsearch failure") + + for { + size, _ := _C_res_nsearch(state, s, class, rtype, buf, bufSize) + if size <= 0 || size > 0xffff { + return nil, errors.New("res_nsearch failure") + } + if size <= bufSize { + break + } + + // Allocate a bigger buffer to fit the entire msg. + _C_free(unsafe.Pointer(buf)) + bufSize = size + buf = (*_C_uchar)(_C_malloc(uintptr(bufSize))) } + var p dnsmessage.Parser - if _, err := p.Start(unsafe.Slice((*byte)(unsafe.Pointer(buf)), size)); err != nil { + if _, err := p.Start(unsafe.Slice((*byte)(unsafe.Pointer(buf)), bufSize)); err != nil { return nil, err } p.SkipAllQuestions() |