Skip to content

x/tools/go/analysis/passes/printf: false positive breaks tests in package #69831

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

Closed
mark-pictor-csec opened this issue Oct 9, 2024 · 8 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@mark-pictor-csec
Copy link

Go version

go1.23, go1.22, tip

Output of go env in your module/workspace:

N/A (repro in playground)

What did you do?

I wrote a function which takes optional args of type any; if the first optional arg is of type string, it is passed to fmt.Sprintf as the format.

The vet check believes I'm incorrectly providing a format specifier; until I found a way to defeat the check, it prevented any tests within the package from running.

https://go.dev/play/p/b6AupyJEr6s

printf analyzer documentation is available on pkg.go.dev

What did you see happen?

The printf checker produces a false positive, and there doesn't seem to be a way to disable it. I was unable to run any tests in the affected package until I found a way to defeat the checker.

What did you expect to see?

go test and the playground compile and execute the code.

@seankhliao seankhliao changed the title golang.org/x/tools/go/analysis/passes/printf: false positive breaks tests in package x/tools/go/analysis/passes/printf: false positive breaks tests in package Oct 9, 2024
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Oct 9, 2024
@gopherbot gopherbot added this to the Unreleased milestone Oct 9, 2024
@zigo101

This comment was marked as resolved.

@timothy-king
Copy link
Contributor

cc @adonovan

@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 10, 2024
@adonovan
Copy link
Member

The printf checker produces a false positive, and there doesn't seem to be a way to disable it. I was unable to run any tests in the affected package until I found a way to defeat the checker.

I appreciate that this change was quite inconvenient for you, but I will note that it is very confusing to declare a function that behaves like either Printf or Print depending on whether the first argument is a string--not only for static analysis tools like vet but for human readers too. I suggest you use this opportunity to rewrite the function to always behave like Sprintf, and give its name an f suffix.

@mark-pictor-csec
Copy link
Author

Commenting out lines 32 and 33 does cause the check to pass, which I was not at all expecting. Thanks for the hint!

@mark-pictor-csec
Copy link
Author

This seems like something that's pretty unlikely to be hit, and difficult to fix. Feel free to close it if you want.

@adonovan
Copy link
Member

To be clear, commenting out L32-33 doesn't really improve the function, as it still exhibits "conditionally printf-like" behavior. It would better if it was either always print-like or always printf-like, as these are well understood paradigms for most Go programmers, and the vet tool will enforce the correct discipline. For example:

// printf-like:

func PrintfIdx(s string, n int, format string, args ...any) {
	if len(s) == 0 {
		return
	}
	x := fmt.Sprintf(format, args...)
	fmt.Fprintf(os.Stderr, "\n%q  %s\n %*s  (%d)\n", s, x, n+1, "^", n)
}

PrintfIdx("", 0, "%s", 123) // <-- vet will point out the mismatch between %s and 123


// print-like:

func PrintIdx(s string, n int, args ...any) {
	if len(s) == 0 {
		return
	}
	x := fmt.Sprint(args...)
	fmt.Fprintf(os.Stderr, "\n%q  %s\n %*s  (%d)\n", s, x, n+1, "^", n)
}

PrintIdx("", 0, "%s", 123) // <-- vet will point out that the first argument looks like an unwanted format string.

@adonovan
Copy link
Member

Working as intended; closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

7 participants