Skip to content

Commit 8555c64

Browse files
committed
[Enhancement]: Optimize insecure random number generation in function util/string.go:RandomString
close: #2696 Signed-off-by: true1064 <tangjingyu@oppo.com>
1 parent 71cbf46 commit 8555c64

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

proto/packet.go

+2
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ func (p *Packet) GetOpMsg() (m string) {
596596
m = "OpLcNodeScan"
597597
case OpLcNodeSnapshotVerDel:
598598
m = "OpLcNodeSnapshotVerDel"
599+
case OpMetaReadDirOnly:
600+
m = "OpMetaReadDirOnly"
599601
default:
600602
m = fmt.Sprintf("op:%v not found", p.Opcode)
601603
}

sdk/data/stream/stream_writer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ begin:
395395
}
396396
}
397397
// try append write, get response
398-
log.LogDebugf("action[streamer.write] doAppendWrite req %v FileOffset %v size %v", req.ExtentKey, req.FileOffset, req.Size)
398+
log.LogDebugf("action[streamer.write] doAppendWrite req: ExtentKey(%v) FileOffset(%v) size(%v)",
399+
req.ExtentKey, req.FileOffset, req.Size)
399400
var status int32
400401
// First, attempt sequential writes using neighboring extent keys. If the last extent has a different version,
401402
// it indicates that the extent may have been fully utilized by the previous version.

util/string.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
package util
1616

1717
import (
18-
"math/rand"
18+
"crypto/rand"
19+
"math/big"
1920
"strings"
20-
"time"
2121
)
2222

2323
func SubString(sourceString string, begin, end int) string {
@@ -59,9 +59,9 @@ func RandomString(length int, seed RandomSeed) string {
5959
runs := seed.Runes()
6060
result := ""
6161
for i := 0; i < length; i++ {
62-
rand.Seed(time.Now().UnixNano())
63-
randNumber := rand.Intn(len(runs))
64-
result += string(runs[randNumber])
62+
lenInt64 := int64(len(runs))
63+
randNumber, _ := rand.Int(rand.Reader, big.NewInt(lenInt64))
64+
result += string(runs[randNumber.Uint64()])
6565
}
6666
return result
6767
}

0 commit comments

Comments
 (0)