From 21147f51b37ebe66889db4548c3d85ebe80761ab Mon Sep 17 00:00:00 2001 From: Pavan Kumar VH Date: Thu, 3 Sep 2026 16:37:07 +0530 Subject: [PATCH] Fix size validation in sampleSizeWithSeed The function didn't validate that size is a non-negative integer. If size was NaN, negative, or not an integer, result.slice(0, size) could behave unexpectedly. Added Number.isFinite() and size >= 0 checks to default to 0 for invalid sizes. --- common/src/util/random.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/src/util/random.ts b/common/src/util/random.ts index de7d5729da..455196d089 100644 --- a/common/src/util/random.ts +++ b/common/src/util/random.ts @@ -5,11 +5,12 @@ export function sampleSizeWithSeed( size: number, seed: string, ): T[] { + const safeSize = Number.isFinite(size) && size >= 0 ? Math.floor(size) : 0 const rng = seedrandom(seed) const result = array.slice() for (let i = result.length - 1; i > 0; i--) { const j = Math.floor(rng() * (i + 1)) ;[result[i], result[j]] = [result[j], result[i]] } - return result.slice(0, size) + return result.slice(0, safeSize) }