Skip to content

Commit

Permalink
fixed unsafe rand instance access
Browse files Browse the repository at this point in the history
  • Loading branch information
rndive committed Jul 3, 2015
1 parent c43832e commit 31162e8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions sockjs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"fmt"
"math/rand"
"net/http"
"sync"
"time"
)

var entropy *rand.Rand
var (
entropy *rand.Rand
entropyMutex sync.Mutex
)

func init() {
entropy = rand.New(rand.NewSource(time.Now().UnixNano()))
Expand Down Expand Up @@ -71,7 +75,7 @@ func (options *Options) info(rw http.ResponseWriter, req *http.Request) {
Websocket: options.Websocket,
CookieNeeded: options.JSessionID != nil,
Origins: []string{"*:*"},
Entropy: entropy.Int31(),
Entropy: generateEntropy(),
})
case "OPTIONS":
rw.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET")
Expand Down Expand Up @@ -101,3 +105,10 @@ func (options *Options) cookie(rw http.ResponseWriter, req *http.Request) {
options.JSessionID(rw, req)
}
}

func generateEntropy() int32 {
entropyMutex.Lock()
entropy := entropy.Int31()
entropyMutex.Unlock()
return entropy
}

0 comments on commit 31162e8

Please sign in to comment.