You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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.
The text was updated successfully, but these errors were encountered: