text/template: generated slice not initialized to zero #34720
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
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
)?windows/amd64
and
linux/amd64
GOARCH=amd64
go env
OutputWhat did you do?
Try this
Then try this
What did you expect to see?
Initialize the new underlying array of the template generated slice to zero.
What did you see instead?
Not zero.
Code one:
Output:
Code two:
Output:
This should be all zeros except the first three:
The
t.Execute(my, my)
callsfunc (m *myWriter) Write(p []byte)
so thep
withlen=3
andcap=128
generated by the tamplate engine.After debugging the 2nd code inside
/usr/local/go/src/fmt/print.go
file at line 230, it seems it isfmt.buffer
withlength=3
, andcap=128
, here:The call to
p := newPrinter()
initialized herep.fmt.init(&p.buf)
:Gets and returns the free memory without initializing it to zero.
Solution:
In
/usr/local/go/src/fmt/format.go
file at line 58 set the buffer to all zero:Then Output of the 2nd code is all zero:
The text was updated successfully, but these errors were encountered: