From 66b07453d73ad5b6c1bd6ee86db24fc97fa5c490 Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Wed, 19 Feb 2025 10:16:07 -0600 Subject: [PATCH] fix random integer generator division by zero --- testutil/sample/sample.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index a143ecefa9..99dd62844c 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -90,22 +90,34 @@ func Coins() sdk.Coins { // Uint64InRange returns a sample uint64 in the given ranges func Uint64InRange(low, high uint64) uint64 { r := newRandFromSeed(int64(low)) + if low == high { + return low // avoid division by zero + } return r.Uint64()%(high-low) + low } // Uint64InRange returns a sample uint64 in the given ranges func Uint64InRangeFromRand(r *rand.Rand, low, high uint64) uint64 { + if low == high { + return low // avoid division by zero + } return r.Uint64()%(high-low) + low } // Int64InRange returns a sample int64 in the given ranges func Int64InRange(low, high int64) int64 { r := newRandFromSeed(low) + if low == high { + return low // avoid division by zero + } return r.Int63()%(high-low) + low } // Int64InRangeFromRand returns a sample int64 in the given ranges func Int64InRangeFromRand(r *rand.Rand, low, high int64) int64 { + if low == high { + return low // avoid division by zero + } return r.Int63()%(high-low) + low }