os: limit temp file randomness to uint32

CL 516860 accidentally changed the randomness
used in TempFile from 32 to 64 bits on 64-bit platforms,
meaning from 10 to 20 decimal bytes.
This is enough to cause problems in a few tests
because it makes temporary directory names just
a little bit longer.

Limit back down to 32 bits of randomness, which is fine,
and add a test to avoid repeating the mistake.

Fixes #64605.

Change-Id: I17b8c063d11d5c0a96a68b5e5f83c889a13bca77
Reviewed-on: https://go-review.googlesource.com/c/go/+/548635
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Russ Cox <rsc@golang.org>
This commit is contained in:
Russ Cox 2023-12-09 14:32:10 -05:00 committed by Gopher Robot
parent 46ea4ab5cb
commit ba7c4e47b3
2 changed files with 25 additions and 1 deletions

View File

@ -3330,3 +3330,27 @@ func TestPipeCloseRace(t *testing.T) {
t.Errorf("got nils %d errs %d, want 2 2", nils, errs)
}
}
func TestRandomLen(t *testing.T) {
for range 5 {
dir, err := MkdirTemp(t.TempDir(), "*")
if err != nil {
t.Fatal(err)
}
base := filepath.Base(dir)
if len(base) > 10 {
t.Errorf("MkdirTemp returned len %d: %s", len(base), base)
}
}
for range 5 {
f, err := CreateTemp(t.TempDir(), "*")
if err != nil {
t.Fatal(err)
}
base := filepath.Base(f.Name())
f.Close()
if len(base) > 10 {
t.Errorf("CreateTemp returned len %d: %s", len(base), base)
}
}
}

View File

@ -19,7 +19,7 @@ import (
func runtime_rand() uint64
func nextRandom() string {
return itoa.Uitoa(uint(runtime_rand()))
return itoa.Uitoa(uint(uint32(runtime_rand())))
}
// CreateTemp creates a new temporary file in the directory dir,