golang/go

cmd/compile: -d=checkptr doesn't detect invalid pointer fields in converted pointers-to-structs

Open

#36.017 aberto em 6 de dez. de 2019

Ver no GitHub
 (17 comments) (0 reactions) (0 assignees)Go (19.008 forks)batch import
NeedsInvestigationcompiler/runtimehelp wanted

Métricas do repositório

Stars
 (133.883 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

What version of Go are you using (go version)?

What operating system and processor architecture are you using (go env)?

What did you do?

package tmp

import (
	"testing"
	"unsafe"
)

func TestUnsafeCast(t *testing.T) {
	var x uint32 = 0
	p := (*uint64)(unsafe.Pointer(&x))
	t.Log(*p)
}

func TestUnsafeStructCast(t *testing.T) {
	var x uint32 = 0

	type A struct {
		p *uint32
	}
	type B struct {
		p *uint64
	}

	v := &A{ptr:&x}
	p := (*B)(unsafe.Pointer(v))
	t.Log(*p.p)
}

What did you expect to see?

Both TestUnsafeCast and TestUnsafeStructCast fail with go test -gcflags=-d=checkptr, since both use invalid unsafe.Pointer casts.

What did you see instead?

Only the TestUnsafeCast fails. Pointer fields are not verified properly.

Guia do colaborador