Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staticcheck: SA5011 believes logrus.Fatal returns #656

Closed
tendervittles opened this issue Nov 15, 2019 · 3 comments
Closed

staticcheck: SA5011 believes logrus.Fatal returns #656

tendervittles opened this issue Nov 15, 2019 · 3 comments

Comments

@tendervittles
Copy link

$ staticcheck -debug.version
staticcheck (devel, v0.0.0-20191107024926-a9480a3ec3bc)

Compiled with Go version: go1.13.4
Main module:
    honnef.co/go/tools@v0.0.0-20191107024926-a9480a3ec3bc (sum: h1:G3KJU7T3tdNpGfKsED8OHHsQozNxEW0rDS785ks+feY=)
Dependencies:
    github.com/BurntSushi/toml@v0.3.1 (sum: h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=)
    golang.org/x/tools@v0.0.0-20191114222411-4191b8cbba09 (sum: h1:f3ZhcxGnJxVhcsQgKPvfAg2pjdeWBBgDuO5XfmGnoU0=)
$ go version
go version go1.13.4 linux/amd64
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tendervittles/.cache/go-build"
GOENV="/home/tendervittles/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tendervittles/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/tendervittles/SA/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build470499691=/tmp/go-build -gno-record-gcc-switches"

Ran staticcheck ./... against:

package main

import (
	"fmt"

	"github.com/sirupsen/logrus"
)

func main() {
	var x *int
	if x == nil {
		logrus.Fatal("Uh-oh")
	}
	fmt.Println(*x)
}

Staticcheck returned:

sa.go:14:14: possible nil pointer dereference (SA5011)
	sa.go:11:5: this check suggests that the pointer can be nil

This version does not trigger the warning:

package main

import (
	"fmt"
	"log"
)

func main() {
	var x *int
	if x == nil {
		log.Fatal("Uh-oh")
	}
	fmt.Println(*x)
}
@tendervittles tendervittles added false-positive needs-triage Newly filed issue that needs triage labels Nov 15, 2019
@dominikh
Copy link
Owner

The issue is that logrus.Fatal does not unconditionally terminate the process, as it relies on logrus.Logger.ExitFunc – which may be any function.

I think my only choice is to add a special case for logrus.

@dominikh dominikh removed the needs-triage Newly filed issue that needs triage label Nov 16, 2019
@mpictor
Copy link

mpictor commented Jun 23, 2021

It'd be nice to have some sort of annotation we could add to functions to tell the analyzer to treat them as non-returning. I'm seeing the same linter error, SA5011, with a call to an in-house logging package's Fatal() function. As with logrus, the actual behavior depends on a function variable.

Clang-analyzer uses __attribute__((analyzer_noreturn)) to emphasize that the attribute impacts analysis only and not compilation, as opposed to the GCC attribute noreturn.

Would it be possible to detect a comment line like // analyzer_noreturn when it's the last (or only) line of a function's block comment, then mark the function as noreturn?

@dominikh
Copy link
Owner

I'm thinking about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants