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

proposal: go/analysis/passes/printf: disallow %d formatting of pointers #62595

Open
adonovan opened this issue Sep 12, 2023 · 0 comments
Open
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) Proposal
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Sep 12, 2023

fmt.Sprintf("%d", new(int)) prints a string of decimal digits, which appears in practice to be almost always a mistake caused by forgetting to dereference a pointer to an integer. (Is there ever a good reason to print a pointer value in decimal?)

In this case the vet check is consistent with the behavior of fmt, but arguably the vet check should be intentionally stricter than fmt (since we can't change fmt). I propose to make vet report the use of %d with a pointer as an error.

It could additionally hint the user to either dereference the pointer (if it's a pointer-to-int), or use "%d" on uintptr(unsafe.Pointer(ptr)), if the decimal string really was wanted.

@adonovan adonovan added Proposal Analysis Issues related to static analysis (vet, x/tools/go/analysis) labels Sep 12, 2023
@ianlancetaylor ianlancetaylor changed the title go/analysis/passes/printf: disallow %d formatting of pointers proposal: go/analysis/passes/printf: disallow %d formatting of pointers Sep 12, 2023
@gopherbot gopherbot added this to the Proposal milestone Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) Proposal
Projects
Status: Incoming
Development

No branches or pull requests

2 participants