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

fmt: custom formatters do not work for struct fields #66340

Closed
kkarganov opened this issue Mar 15, 2024 · 3 comments
Closed

fmt: custom formatters do not work for struct fields #66340

kkarganov opened this issue Mar 15, 2024 · 3 comments

Comments

@kkarganov
Copy link

Go version

go 1.22

Output of go env in your module/workspace:

GOROOT='/opt/homebrew/Cellar/go/1.22.0/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.0/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/n7/l86gpr1x5vb55kqd_k4g1mq00000gq/T/go-build852953591=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

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

For the type with Stringer interface custom formatting works only if printing directly this class, and doesn't work if it is a struct member:

type Frac struct {
	a int
	b int
}

func (f Frac) String() string { return fmt.Sprintf("\"%d/%d\"", f.a, f.b) }

func main() {
        f := Frac{1, 3}
	fmt.Printf("Fraction is %v\n", f)

	st := struct {
		one Frac
		two Frac
	}{Frac{1, 3}, Frac{3, 5}}
	fmt.Printf("Struct is %v\n", st)
}

What did you see happen?

printing type as fmt.Printf("Fraction is %v\n", f) calls String() method:

Fraction is "1/3"

printing struct fmt.Printf("Struct is %v\n", st) does not:

Struct is {{1 3} {3 5}}

What did you expect to see?

libexec/src/fmt/print.go:pp.printArg checks for the Printf argument type and if it doesn't have custom formatting methods it goes to print it as value, while struct fields can have their own Stringer/GoStringer implementations

@seankhliao
Copy link
Member

Duplicate of #16698

@seankhliao seankhliao marked this as a duplicate of #16698 Mar 15, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2024
@kkarganov
Copy link
Author

kkarganov commented Mar 15, 2024

This can lead to security issues in go code. Consider the example:
https://go.dev/play/p/rswMHkfgua7

As a developer of security-related module I think all my types are logging-safe.
But in other modules they can just print everything to logs in plain-text.

@ianlancetaylor
Copy link
Contributor

Yes, it is something to be aware of. But it's not something we can change at this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants