fmt: should clearflags also set wid and prec to zero #61913
Labels
FixPending
Issues that have a fix which has not yet been reviewed or submitted.
FrozenDueToAge
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
So I have implemented
fmt.Formatter
interface for my own struct and wrote bunch of tests. In my case, formatting makes sense only for precision 0 and 1, so I have such check in my code:What did you expect to see?
I expected to see that all tests which do not provide precision at all in format strings succeed.
What did you see instead?
I see random failures. After debugging I think the reason is that
newPrinter()
infmt
package uses async.Pool
and reuses printers. The code callsp.fmt.clearflags()
before forsimpleFormat
formats just callsprintArg
which callshandleMethods
. Because it is a simple format precision is never parsed nor set (justprecPresent
is reset). Because I do not check forok
value froms.Precision()
but just useprecision
I think I get precision from some other printer instance when it was set in the past.I find this surprising. In general my experience in Go was that uninitialized values are always 0. So even if precision has not been set I would expect it always to be 0. This happens only because instances are cached (an optimization).
I would suggest that
clearflags
simply setswid
andprec
fields to zero, too. Maybeclearflags
should also just be renamed toclear
?I am not sure how to write a test case or reproduction for this.
The text was updated successfully, but these errors were encountered: