]> Cypherpunks.ru repositories - gostls13.git/commitdiff
os: limit temp file randomness to uint32
authorRuss Cox <rsc@golang.org>
Sat, 9 Dec 2023 19:32:10 +0000 (14:32 -0500)
committerGopher Robot <gobot@golang.org>
Mon, 11 Dec 2023 15:36:54 +0000 (15:36 +0000)
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>

src/os/os_test.go
src/os/tempfile.go

index 7e0e0b90be0b56c87801695982e5560230f24f3d..2f5b117bd90ff57aa9a4add054e5a8a2cb87f87d 100644 (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)
+               }
+       }
+}
index 7f2b6a883cc3a09ca06fed9bec00ad67513b2a3e..66c65e6c783c745a98e1fbca3b318fa642bfaa3f 100644 (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,