From 13b6c1e150fbc4f51e39c5a8bc76a6b2f72d4243 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 21 Mar 2022 13:56:43 +0200 Subject: [PATCH] Fix max number of samples in experiments on non-64-bit systems. The use of `math.MaxInt64` as the maximum number of samples in the context of general int variables can lead to inconsistent behavior on non-64-bit systems. This patch addresses this using the arbitrary-precision `math/big.Int` type to store and compare sampling numbers to ensure no architecture-dependant int size mismatches will occur. Signed-off-by: Nashwan Azhari --- gmeasure/experiment.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/gmeasure/experiment.go b/gmeasure/experiment.go index 2ac521d73..bc8fb7931 100644 --- a/gmeasure/experiment.go +++ b/gmeasure/experiment.go @@ -40,6 +40,7 @@ package gmeasure import ( "fmt" "math" + "math/big" "reflect" "sync" "time" @@ -452,9 +453,11 @@ func (e *Experiment) Sample(callback func(idx int), samplingConfig SamplingConfi if samplingConfig.Duration > 0 { maxTime = time.Now().Add(samplingConfig.Duration) } - maxN := math.MaxInt64 + maxN := big.NewInt(math.MaxInt64) if samplingConfig.N > 0 { - maxN = samplingConfig.N + // NOTE(aznashwan): we print then parse the samplingConfig.N to future-proof + // against variations in the bit size of the standard int type: + maxN.SetString(fmt.Sprintf("%d", samplingConfig.N), 10) } numParallel := 1 if samplingConfig.NumParallel > numParallel { @@ -491,7 +494,12 @@ func (e *Experiment) Sample(callback func(idx int), samplingConfig SamplingConfi avgDt = (avgDt*time.Duration(idx-numParallel) + dt) / time.Duration(idx-numParallel+1) } idx += 1 - if idx >= maxN { + + idxBigInt := big.NewInt(0) + // NOTE(aznashwan): we print then parse the integer idx to future-proof + // against variations in the bit size of the standard int type: + idxBigInt.SetString(fmt.Sprintf("%d", idx), 10) + if idxBigInt.Cmp(maxN) >= 0 { return } if time.Now().Add(avgDt).After(maxTime) {