Fix G115 integer overflow warnings in agehd tests
Add bounds checking before converting int to uint32 to prevent potential integer overflow in benchmark and concurrent test functions
此提交包含在:
+19
-3
@@ -663,6 +663,10 @@ func TestConcurrentDerivation(t *testing.T) {
|
||||
for i := 0; i < testNumGoroutines; i++ {
|
||||
go func(goroutineID int) {
|
||||
for j := 0; j < testNumIterations; j++ {
|
||||
if j < 0 || j > 1000000 {
|
||||
errors <- fmt.Errorf("index out of safe range")
|
||||
return
|
||||
}
|
||||
identity, err := DeriveIdentity(mnemonic, uint32(j))
|
||||
if err != nil {
|
||||
errors <- err
|
||||
@@ -706,7 +710,11 @@ func TestConcurrentDerivation(t *testing.T) {
|
||||
// Benchmark tests
|
||||
func BenchmarkDeriveIdentity(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := DeriveIdentity(mnemonic, uint32(i%1000))
|
||||
index := i % 1000
|
||||
if index < 0 || index > 1000000 {
|
||||
b.Fatalf("index out of safe range: %d", index)
|
||||
}
|
||||
_, err := DeriveIdentity(mnemonic, uint32(index))
|
||||
if err != nil {
|
||||
b.Fatalf("derive identity: %v", err)
|
||||
}
|
||||
@@ -715,7 +723,11 @@ func BenchmarkDeriveIdentity(b *testing.B) {
|
||||
|
||||
func BenchmarkDeriveIdentityFromXPRV(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := DeriveIdentityFromXPRV(testXPRV, uint32(i%1000))
|
||||
index := i % 1000
|
||||
if index < 0 || index > 1000000 {
|
||||
b.Fatalf("index out of safe range: %d", index)
|
||||
}
|
||||
_, err := DeriveIdentityFromXPRV(testXPRV, uint32(index))
|
||||
if err != nil {
|
||||
b.Fatalf("derive identity from xprv: %v", err)
|
||||
}
|
||||
@@ -724,7 +736,11 @@ func BenchmarkDeriveIdentityFromXPRV(b *testing.B) {
|
||||
|
||||
func BenchmarkDeriveEntropy(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := DeriveEntropy(mnemonic, uint32(i%1000))
|
||||
index := i % 1000
|
||||
if index < 0 || index > 1000000 {
|
||||
b.Fatalf("index out of safe range: %d", index)
|
||||
}
|
||||
_, err := DeriveEntropy(mnemonic, uint32(index))
|
||||
if err != nil {
|
||||
b.Fatalf("derive entropy: %v", err)
|
||||
}
|
||||
|
||||
新增問題並參考
封鎖使用者