]> Cypherpunks repositories - gostls13.git/commitdiff
io/ioutil: fix data race on rand
authorDmitriy Vyukov <dvyukov@google.com>
Tue, 9 Oct 2012 17:08:53 +0000 (21:08 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Tue, 9 Oct 2012 17:08:53 +0000 (21:08 +0400)
Fixes #4212.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6641050

src/pkg/io/ioutil/tempfile.go

index 42d2e6758698186a0724f262199b4de5d3d2925f..257e05d2152da8d54a00705d778034092779a3fc 100644 (file)
@@ -8,6 +8,7 @@ import (
        "os"
        "path/filepath"
        "strconv"
+       "sync"
        "time"
 )
 
@@ -16,18 +17,21 @@ import (
 // chance the file doesn't exist yet - keeps the number of tries in
 // TempFile to a minimum.
 var rand uint32
+var randmu sync.Mutex
 
 func reseed() uint32 {
        return uint32(time.Now().UnixNano() + int64(os.Getpid()))
 }
 
 func nextSuffix() string {
+       randmu.Lock()
        r := rand
        if r == 0 {
                r = reseed()
        }
        r = r*1664525 + 1013904223 // constants from Numerical Recipes
        rand = r
+       randmu.Unlock()
        return strconv.Itoa(int(1e9 + r%1e9))[1:]
 }