这里有一个简单的例子:
package main
import "fmt"
type A struct {
Name *string
}
type B struct {
A *A
}
func main() {
deref(&B{})
}
func deref(b *B) {
fmt.Println(*b.A.Name) // panic: nil pointer dereference
}
我正在寻找一种可以发现这种情况的解决方案。有一个";零性检验";在Jetbrains Goland,但它没有检查这个问题。
我在其他地方问了你的问题[1],得到的回答是:
检测所有CCD_ 1指针解引用是不可能的,我们通常不会容忍假阳性。这也是公平的计算成本高昂。
检测像这样琐碎的CCD_ 2指针解引用是不值得的,因为即使对代码进行了少量添加,它们也不再容易发现
我们有SA5011,它标记特定类型的潜在
nil
指针取消引用,尽管这没有帮助以你的例子。
- https://github.com/dominikh/go-tools/issues/1035