From: Russ Cox Date: Sat, 9 Dec 2023 19:32:10 +0000 (-0500) Subject: os: limit temp file randomness to uint32 X-Git-Tag: go1.22rc1~51 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ba7c4e47b32fa99db8aa05331426b328bb39517a;p=gostls13.git 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 LUCI-TryBot-Result: Go LUCI Auto-Submit: Russ Cox --- diff --git a/src/os/os_test.go b/src/os/os_test.go index 7e0e0b90be..2f5b117bd9 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -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) + } + } +} diff --git a/src/os/tempfile.go b/src/os/tempfile.go index 7f2b6a883c..66c65e6c78 100644 --- a/src/os/tempfile.go +++ b/src/os/tempfile.go @@ -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,