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

aggressive: []byte from netip.Addr.AsSlice should not be printed with %s #1503

Open
ainar-g opened this issue Mar 6, 2024 · 0 comments
Open
Labels
aggressive A set of checks that is more prone to false positives but is helpful during code review new-check

Comments

@ainar-g
Copy link
Contributor

ainar-g commented Mar 6, 2024

(This could technically be a part of a larger feature regarding printability of []byte, but I've decided to only file the one I've met in the wild. Feel free to retitle/expand.)

Initial code:

ip := net.ParseIP(s)
process(ip)
processAsSlice(ip)
fmt.Printf("address: %s\n", ip)

With processAsSlice accepting []byte.

After some time, process starts to accept netip.Addr instead of net.IP. So, a refactoring is made:

addr := netip.MustParseAddr(s)
ip := addr.AsSlice()
process(addr)
processAsSlice(ip)
fmt.Printf("address: %s\n", ip)

This looks okay, except that netip.Addr.AsSlice returns []byte, not net.IP. So, fmt.Printf doesn't print, say, 1.2.3.4, but rather the bytes with the values \x01\x02\x03\x04.

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

There is technically a possibility that someone actually wants to print those bytes, so I'm proposing this one for the aggressive mode, but I also feel like 99 % of this sort of code is a bug.

@ainar-g ainar-g added the needs-triage Newly filed issue that needs triage label Mar 6, 2024
@dominikh dominikh added new-check aggressive A set of checks that is more prone to false positives but is helpful during code review and removed needs-triage Newly filed issue that needs triage labels Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aggressive A set of checks that is more prone to false positives but is helpful during code review new-check
Projects
None yet
Development

No branches or pull requests

2 participants