]> Cypherpunks repositories - gostls13.git/commitdiff
testing: limit TempDir name length
authorSean Liao <sean@liao.dev>
Sun, 11 May 2025 21:36:58 +0000 (22:36 +0100)
committerSean Liao <sean@liao.dev>
Mon, 12 May 2025 19:34:50 +0000 (12:34 -0700)
Fixes #71742

Change-Id: Ibef8f7f0a36b25f181062c4d2f84279a97e467a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/671577
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/testing/testing.go
src/testing/testing_test.go

index 78681b605b1d21287c383d9b2f2ac8c656ccebbd..d50abea32f2d3ddd70e929ae70a395dceb5e61b5 100644 (file)
@@ -1244,6 +1244,11 @@ func (c *common) TempDir() string {
        if nonExistent {
                c.Helper()
 
+               pattern := c.Name()
+               // Limit length of file names on disk.
+               // Invalid runes from slicing are dropped by strings.Map below.
+               pattern = pattern[:min(len(pattern), 64)]
+
                // Drop unusual characters (such as path separators or
                // characters interacting with globs) from the directory name to
                // avoid surprising os.MkdirTemp behavior.
@@ -1263,7 +1268,7 @@ func (c *common) TempDir() string {
                        }
                        return -1
                }
-               pattern := strings.Map(mapper, c.Name())
+               pattern = strings.Map(mapper, pattern)
                c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern)
                if c.tempDirErr == nil {
                        c.Cleanup(func() {
index 907d0701f033d4b1a173aeabfef34cb87f1da06c..209291d3228532e25bd2718b47e315b413d89e34 100644 (file)
@@ -89,6 +89,7 @@ func TestTempDir(t *testing.T) {
        t.Run("test[]", testTempDir)
        t.Run("test*", testTempDir)
        t.Run("äöüéè", testTempDir)
+       t.Run(strings.Repeat("a", 300), testTempDir)
 }
 
 func testTempDir(t *testing.T) {