使用空值检查器进行静态分析

huangapple go评论73阅读模式
英文:

Using the nilness checker for static analysis

问题

我想使用nilness checker来捕捉空指针解引用。我已经安装了该检查器并构建了nilness命令:

$ ls src/golang.org/x/tools/go/analysis/passes/nilness/
cmd  nilness.go  nilness_test.go  testdata
$ ls bin/nilness
bin/nilness
$ ls -l bin/nilness
-rwxrwxr-x. 1 ... ... 10291396 Jun 20 22:00 bin/nilness

但是在一个已知存在错误的目录或文件上运行nilness命令没有输出。

作为替代方法,按照标准指南,我创建了这个程序:

package main

import (
    "golang.org/x/tools/go/analysis/multichecker"
    "golang.org/x/tools/go/analysis/passes/loopclosure"
    "golang.org/x/tools/go/analysis/passes/nilness"
)

func main() {
    multichecker.Main(
        nilness.Analyzer,
        loopclosure.Analyzer,
    )
}

这个程序也没有捕捉到空指针解引用。

有人使用过这个检查器吗?我漏掉了什么吗?

英文:

I'd like to use the nilness checker to catch nil pointer dereferences. I installed the checker and built the nilness command:

$ ls src/golang.org/x/tools/go/analysis/passes/nilness/
cmd  nilness.go  nilness_test.go  testdata
$ ls bin/nilness
bin/nilness
$ ls -l bin/nilness
-rwxrwxr-x. 1 ... ... 10291396 Jun 20 22:00 bin/nilness

But running nilness on a directory or file with known errors produces no output.

As an alternative approach, following the standard guidelines, I created this program:

package main

import (
    "golang.org/x/tools/go/analysis/multichecker"
    "golang.org/x/tools/go/analysis/passes/loopclosure"
    "golang.org/x/tools/go/analysis/passes/nilness"
)

func main() {
    multichecker.Main(
        nilness.Analyzer,
        loopclosure.Analyzer,
    )
}

This does not catch nil pointer dereferences either.

Has anybody used this checker? What am I missing?

答案1

得分: 1

我认为它应该可以正常工作。请仔细检查您正在运行“nilness”分析器的测试数据。

让我给您展示一个示例:

example/main.go

package nilness

func nilness() {
	var t *int
	if t == nil {
		println(*t) // 报错
	}
}

nilness 分析器 (main.go)

package main

import (
	"golang.org/x/tools/go/analysis/passes/nilness"
	"golang.org/x/tools/go/analysis/singlechecker"
)

func main() {
	singlechecker.Main(nilness.Analyzer)
}

运行:

$ go build -o checker
$ ./checker ./...

输出:

<path>/example/main.go:5:7: 无意义的条件:nil == nil
<path>/example/main.go:6:11: 加载时的 nil 解引用

正如 @Volker 已经指出的,nilness 分析器只能检测程序中静态可证明的空指针引用,因此通常很少。也许这就是它对您不起作用的原因。

英文:

I think it should work fine. Please double-check the test data on which you are running the nilness analyzer.

Let me show you a sample example:

example/main.go

package nilness

func nilness() {
	var t *int
	if t == nil {
		println(*t) // raise
	}
}

nilness analyzer (main.go)

package main

import (
	&quot;golang.org/x/tools/go/analysis/passes/nilness&quot;
	&quot;golang.org/x/tools/go/analysis/singlechecker&quot;
)

func main() {
	singlechecker.Main(nilness.Analyzer)
}

Run:

$ go build -o checker
$ ./checker ./...

Output:

&lt;path&gt;/example/main.go:5:7: tautological condition: nil == nil
&lt;path&gt;/example/main.go:6:11: nil dereference in load

As @Volker already pointed out that the nilness analyzer only detects the statically provable nil pointer references in your program, so there are usually very less. Maybe this is the reason it is not working for you.

huangapple
  • 本文由 发表于 2021年6月21日 13:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/68062809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定