fix(util): return full uint64 randomness (#9864)

Problem: RandomUint64 generated eight random bytes but returned int32, truncating the value before mount file and directory handles converted it to uint64. This reduced handle entropy to 32 bits and produced sign-extended handle values.\n\nRoot cause: the helper cast BytesToUint64 to int32 and exposed int32 as its return type.\n\nFix: make RandomUint64 return uint64 and return the full BytesToUint64 result.\n\nReproduction: go test ./weed/util -run TestRandomUint64ReturnsUint64 -count=1 failed before the fix because RandomUint64() had kind int32.\n\nValidation: gofmt -w weed/util/bytes.go weed/util/bytes_test.go; git diff --check; go test ./weed/util -run TestRandomUint64ReturnsUint64 -count=1; go test ./weed/util -count=1; go test ./weed/mount -count=1; git diff --cached --check
This commit is contained in:
7y-9
2026-06-09 13:07:24 +08:00
committed by GitHub
parent 4a96f624d9
commit 7bbd28634a
2 changed files with 47 additions and 4 deletions
+7 -3
View File
@@ -13,6 +13,8 @@ import (
"unicode"
)
var randomRead = rand.Read
// BytesToHumanReadable returns the converted human readable representation of the bytes.
func BytesToHumanReadable(b uint64) string {
const unit = 1024
@@ -156,10 +158,12 @@ func RandomInt32() int32 {
return int32(BytesToUint32(buf))
}
func RandomUint64() int32 {
func RandomUint64() uint64 {
buf := make([]byte, 8)
rand.Read(buf)
return int32(BytesToUint64(buf))
if _, err := randomRead(buf); err != nil {
panic(err)
}
return BytesToUint64(buf)
}
func RandomBytes(byteCount int) []byte {
+40 -1
View File
@@ -1,6 +1,45 @@
package util
import "testing"
import (
"errors"
"testing"
)
func TestRandomUint64ReturnsFullRandomValue(t *testing.T) {
useRandomRead(t, func(p []byte) (int, error) {
copy(p, []byte{0x80, 0, 0, 0, 0, 0, 0, 1})
return len(p), nil
})
const want uint64 = 0x8000000000000001
if got := RandomUint64(); got != want {
t.Fatalf("RandomUint64() = %d, want %d", got, want)
}
}
func TestRandomUint64PanicsOnRandomReadError(t *testing.T) {
useRandomRead(t, func([]byte) (int, error) {
return 0, errors.New("random read failed")
})
defer func() {
if recover() == nil {
t.Fatal("RandomUint64() did not panic on random read error")
}
}()
RandomUint64()
}
func useRandomRead(t *testing.T, read func([]byte) (int, error)) {
t.Helper()
original := randomRead
randomRead = read
t.Cleanup(func() {
randomRead = original
})
}
func TestByteParsing(t *testing.T) {
tests := []struct {