-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/tools/go/analysis/passes/printf: catches dual use variable strings passed to fmt.Sprintf #70088
Comments
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
One can of course use While I haven't looked at the full source code, the code you show actually looks problematic.
That is going to pass some non-constant value as the first argument to
That is what the new vet check is encouraging. The new vet check was added in #60529. In the discussion there the check reliably found potential bugs. And that is how I would describe this code as well. |
In this case the analyzer has found a genuine problem in the code: the call Perhaps the error message could be clarified, but this is not a false positive.
Indeed, the use of "%s" is appropriate and correct, and I routinely make suggestions of this form in code reviews. [Sorry, I failed to notice @ianlancetaylor already said this.] |
I'm going to close this as "working as intended". Feel free to reopen if you disagree. |
Closed as "working as intended." |
Go version
go version devel go1.24-889abb17e1 Sat Oct 26 02:44:00 2024 +0000 linux/amd64
Output of
go env
in your module/workspace:What did you do?
spanner
, then try to run the testsbut let's examine that code https://github.com/googleapis/google-cloud-go/blob/255c6bfcdd3e844dcf602a829bfa2ce495bcd72e/spanner/transaction.go#L1351
which calls
https://github.com/googleapis/google-cloud-go/blob/255c6bfcdd3e844dcf602a829bfa2ce495bcd72e/spanner/errors.go#L124-L132
The docs for (fmt.Sprintf)[https://pkg.go.dev/fmt#Sprintf] don't specify some strict requirement that the first argument has to be constant and never has over the past 23 Go releases
That code is valid because despite the dual usage, here they just passed in
format
and notargs
. It is a very common pattern to pass in errors generated by RPC systems as errors to observability spans.But even if we passed in arguments to the format specifier, that isn't a fatal problem; instead it is going to break a whole lot of libraries widely used in the ecosystem for example:
What did you see happen?
Failed testing and compilation!
What did you expect to see?
No problems at all.
Kindly cc-ing @alandonovan
The text was updated successfully, but these errors were encountered: