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

cmd/vet: printf %q with pointer argument not detected #25233

Closed
rudis opened this issue May 3, 2018 · 3 comments
Closed

cmd/vet: printf %q with pointer argument not detected #25233

rudis opened this issue May 3, 2018 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rudis
Copy link

rudis commented May 3, 2018

What version of Go are you using (go version)?

go version go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

Yes. Also in Git master as of 2018-05-03.

What did you do?

package main
import (
        "fmt"
)
func main() {
        x := "hi"
        y := &x
        fmt.Printf("%q\n", y)
}

The programs prints %!q(*string=0xc42000e1e0) but go vet prints no error message.

What did you expect to see?

go vet detects the error.

The problem seems to be the type classification which marks %q as argRune|argInt|argString, this causes the check in cmd/vet/types.go matchArgTypeInternal() (under case *types.Pointer) to consider y (from code above) as valid int, although this won't work with pointers for %q.

A potential patch could look like this. However it feels ugly and therefor wasn't submitted as pull request:

--- types.go.orig       2018-05-03 13:06:45.151615943 +0200
+++ types.go    2018-05-03 13:08:09.414532090 +0200
@@ -201,6 +201,10 @@
                if str, ok := typ.Elem().Underlying().(*types.Struct); ok {
                        return f.matchStructArgType(t, str, arg, inProgress)
                }
+               // %q can't print pointers.
+               if t == (argRune|argInt|argString) {
+                       return false
+               }
                // The rest can print with %p as pointers, or as integers with %x etc.
                return t&(argInt|argPointer) != 0
@ALTree
Copy link
Member

ALTree commented May 3, 2018

cc @robpike @mvdan

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 3, 2018
@ALTree ALTree added this to the Go1.11 milestone May 3, 2018
@mvdan
Copy link
Member

mvdan commented May 3, 2018

I've been poking around in the vet codebase for a bit, and I think it's simply inconsistent with fmt's documentation and code. I'm going to submit a CL soon, which solves the issue that @rudis is having, and I believe also fixes other inconsistencies with fmt. The patch is different from the one proposed above, though.

@mvdan mvdan self-assigned this May 3, 2018
@gopherbot
Copy link

Change https://golang.org/cl/111284 mentions this issue: cmd/vet: better align print warnings with fmt

@golang golang locked and limited conversation to collaborators May 4, 2019
@rsc rsc unassigned mvdan Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants