internal/strconv: fix mishandling of long outputs

The computation of middle digit counts was not right,
leading to not initializing some bytes in the output, with
the effect that output bytes were NUL instead of '0'.

Thanks to @rokkerruslan for the analysis, test case,
and suggested fix.

Fixes #79591.

Change-Id: I32a4c53109a3444382e491fda28028b5c2fcf16d
Reviewed-on: https://go-review.googlesource.com/c/go/+/781340
Reviewed-by: Neal Patel <neal@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Neal Patel <nealpatel@google.com>
This commit is contained in:
Russ Cox 2026-05-21 12:24:58 -04:00
parent fc245b6427
commit 99675026a7
2 changed files with 7 additions and 4 deletions

View file

@ -448,13 +448,13 @@ func fmtEFG(dst []byte, neg bool, s []byte, dp, nd, prec int, fmt byte, shortest
// fraction
if prec > 0 {
dst = append(dst, '.')
lz := min(prec, max(0, -dp)) // leading zeros
m := min(prec-lz, max(0, nd-dp)) // middle digits
tz := max(0, prec-lz-m) // trailing zeros
lz := min(prec, max(0, -dp)) // leading zeros
off := dp + lz
m := min(prec-lz, max(0, nd-off)) // middle digits
tz := max(0, prec-lz-m) // trailing zeros
for range lz {
dst = append(dst, '0')
}
off := dp + lz
for i := range m {
dst = append(dst, s[off+i])
}

View file

@ -218,6 +218,9 @@ var ftoatests = []ftoaTest{
{0x1.000000000005p+71, 'e', 16, "2.3611832414348645e+21"},
{0x1.0000p-27, 'e', 17, "7.45058059692382812e-09"},
{0x1.0000p-41, 'e', 17, "4.54747350886464119e-13"},
// go.dev/issue/79591; used NULs instead of trailing zeros
{0.00000000000000000093564868367555, 'f', 150, "0.000000000000000000935648683675550014820074270847655141588693848715273682775661612254225474316626787185668945312500000000000000000000000000000000000000"},
}
func TestFtoa(t *testing.T) {