1
0
mirror of https://github.com/golang/go synced 2024-11-26 12:58:06 -07:00
go/src/net/cgo_unix_cgo.go
Russ Cox 12ab0ac184 net: pass C string to res_nsearch, in case it stores the pointer
The current code passes a Go pointer to a NUL-terminated C string
to the C function res_nsearch (or res_search), but that function may
in turn store the pointer into the res_state, which is a violation of the
cgo pointer rules and is being detected on the linux-amd64-wsl builder.

Allocating the string in C memory is safer and should resolve
the cgo pointer check. When using libc/syscall mode, the memory
is still allocated Go-side, which could potentially be a problem
if we ever add a moving collector. For now it is OK.

Fixes #56658.

Change-Id: Ibd84a9665be16c71994ddb1eedf09d45a6553a3e
Reviewed-on: https://go-review.googlesource.com/c/go/+/448795
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Bypass: Russ Cox <rsc@golang.org>
2022-11-08 20:59:36 +00:00

76 lines
2.4 KiB
Go

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build cgo && !netgo && unix && !darwin
package net
/*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
// If nothing else defined EAI_OVERFLOW, make sure it has a value.
#ifndef EAI_OVERFLOW
#define EAI_OVERFLOW -12
#endif
*/
import "C"
import "unsafe"
const (
_C_AF_INET = C.AF_INET
_C_AF_INET6 = C.AF_INET6
_C_AF_UNSPEC = C.AF_UNSPEC
_C_EAI_AGAIN = C.EAI_AGAIN
_C_EAI_NONAME = C.EAI_NONAME
_C_EAI_OVERFLOW = C.EAI_OVERFLOW
_C_EAI_SYSTEM = C.EAI_SYSTEM
_C_IPPROTO_TCP = C.IPPROTO_TCP
_C_IPPROTO_UDP = C.IPPROTO_UDP
_C_SOCK_DGRAM = C.SOCK_DGRAM
_C_SOCK_STREAM = C.SOCK_STREAM
)
type (
_C_char = C.char
_C_uchar = C.uchar
_C_int = C.int
_C_uint = C.uint
_C_socklen_t = C.socklen_t
_C_struct_addrinfo = C.struct_addrinfo
_C_struct_sockaddr = C.struct_sockaddr
)
func _C_GoString(p *_C_char) string { return C.GoString(p) }
func _C_CString(s string) *_C_char { return C.CString(s) }
func _C_FreeCString(p *_C_char) { C.free(unsafe.Pointer(p)) }
func _C_malloc(n uintptr) unsafe.Pointer { return C.malloc(C.size_t(n)) }
func _C_free(p unsafe.Pointer) { C.free(p) }
func _C_ai_addr(ai *_C_struct_addrinfo) **_C_struct_sockaddr { return &ai.ai_addr }
func _C_ai_canonname(ai *_C_struct_addrinfo) **_C_char { return &ai.ai_canonname }
func _C_ai_family(ai *_C_struct_addrinfo) *_C_int { return &ai.ai_family }
func _C_ai_flags(ai *_C_struct_addrinfo) *_C_int { return &ai.ai_flags }
func _C_ai_next(ai *_C_struct_addrinfo) **_C_struct_addrinfo { return &ai.ai_next }
func _C_ai_protocol(ai *_C_struct_addrinfo) *_C_int { return &ai.ai_protocol }
func _C_ai_socktype(ai *_C_struct_addrinfo) *_C_int { return &ai.ai_socktype }
func _C_freeaddrinfo(ai *_C_struct_addrinfo) {
C.freeaddrinfo(ai)
}
func _C_gai_strerror(eai _C_int) string {
return C.GoString(C.gai_strerror(eai))
}
func _C_getaddrinfo(hostname, servname *_C_char, hints *_C_struct_addrinfo, res **_C_struct_addrinfo) (int, error) {
x, err := C.getaddrinfo(hostname, servname, hints, res)
return int(x), err
}