mosn / envoy-go-extension

evnoy with go extension
Apache License 2.0
42 stars 10 forks source link

Unused code gets `go vet` error #7

Open spacewander opened 2 years ago

spacewander commented 2 years ago
 go vet ./...
# mosn.io/envoy-go-extension/pkg/utils
pkg/utils/string.go:12:22: possible misuse of unsafe.Pointer
pkg/utils/string.go:20:22: possible misuse of unsafe.Pointer
# mosn.io/envoy-go-extension/pkg/http
pkg/http/capi.go:36:79: possibly passing Go type with embedded pointer to C

I have checked all the errors, and it seems they come from unused file or argument, so we can simply remove them.

    C.moeHttpSendLocalReply(r, C.int(response_code), unsafe.Pointer(&body_text), unsafe.Pointer(&strs), C.longlong(grpc_status), unsafe.Pointer(&details))

We can pass the addr of a flat buffer instead of []string to avoid possibly passing Go type with embedded pointer to C

    sHdr.Data = uintptr(unsafe.Pointer(uintptr(ptr)))

It seems we can simply write sHdr.Data = uintptr(ptr) in this case? Do I miss something?

doujiang24 commented 2 years ago

@spacewander Nice catch~

  1. for possible misuse of unsafe.Pointer yes, it's a misuse. Patch welcome ~

  2. for possibly passing Go type with embedded pointer to C it's an expected warning. It's unsafe usage for cgo, which is also desired. (We also disabled cgocheck for unsafe passing Go pointer from go to c and c to go).

We can pass the addr of a flat buffer instead of []string

this could be a proper way to mute this vet warning, but I'm not sure if it's worthing, it may complicate the code. we need to reflect the flat buffer into []string before using it as strings. thoughts?

spacewander commented 2 years ago

this could be a proper way to mute this vet warning, but I'm not sure if it's worthing, it may complicate the code.

What about passing the Data ptr like: https://github.com/mosn/envoy-go-extension/blob/c20a07ecb9add915f8f8f17f499bdf95949ac549/pkg/http/capi.go#L46-L55

spacewander commented 2 years ago

Actually we can delay the possibly passing Go type with embedded pointer to C suppression as the headers argument is not used at all. 😃