{"record":{"id":"33f7a07bd3e392e0","repo":"different-ai/openwork","slug":"string-repeat-expects-a-finite-non-negative-count","errorCode":null,"errorMessage":"String.repeat expects a finite non-negative count.","messagePattern":"String\\.repeat expects a finite non-negative count\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/interpreter/runtime.ts","lineNumber":469,"sourceCode":"      const pattern = toHostRegex(args[0], name, node, \"g\")\n      if (!pattern.global) {\n        throw new InterpreterRuntimeError(\n          `String.matchAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.match for a single match.`,\n          node,\n        )\n      }\n      // Materialized as an array (not an iterator); each entry is a match array with\n      // index/groups own properties. Match count is bounded by the subject length.\n      return Array.from(value.matchAll(pattern), matchToValue)\n    }\n    case \"search\": {\n      result = value.search(toHostRegex(args[0], name, node))\n      break\n    }\n    case \"repeat\": {\n      const count = num(0)\n      if (!Number.isFinite(count) || count < 0)\n        throw new InterpreterRuntimeError(\"String.repeat expects a finite non-negative count.\", node)\n      result = value.repeat(count)\n      break\n    }\n    case \"padStart\":\n      result = value.padStart(num(0), optStr(1))\n      break\n    case \"padEnd\":\n      result = value.padEnd(num(0), optStr(1))\n      break\n    case \"charAt\":\n      result = value.charAt(optNum(0) ?? 0)\n      break\n    case \"at\":\n      result = value.at(optNum(0) ?? 0)\n      break\n    case \"substring\":\n      result = value.substring(optNum(0) ?? 0, optNum(1))\n      break","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/interpreter/runtime.ts#L451-L487","documentation":"String.repeat's count argument is validated beyond typeof: it must be a finite number >= 0. The host throws RangeError for negative or infinite counts (and for results exceeding max string length); the sandbox throws its own InterpreterRuntimeError first with a clearer message. NaN also fails the Number.isFinite check.","triggerScenarios":"Calling str.repeat(-1), str.repeat(Infinity), str.repeat(NaN), or a computed count like str.repeat(n - m) where the subtraction went negative; repeat with a count derived from user input or an empty array length unexpectedly 0 is fine, but a negative computed offset is common.","commonSituations":"Repeat counts computed from differences (end - start) with inverted bounds; unvalidated user-specified repetition counts; NaN from parseFloat of bad input; off-by-one making the count -1 on empty input.","solutions":["Clamp the count: Math.max(0, Math.floor(count)) before calling repeat","Validate the computed count with Number.isFinite(n) && n >= 0 before the call","Check the source of the count for NaN (failed numeric parse) and negative diffs (swapped bounds)","Guard empty-input cases that make the count negative"],"exampleFix":"// before\nconst pad = \" \".repeat(width - text.length)\n// after\nconst count = Math.max(0, Math.floor(width - text.length))\nconst pad = \" \".repeat(count)","handlingStrategy":"validation","validationCode":"function safeCount(n) { const c = Number(n); if (!Number.isFinite(c) || c < 0) throw new TypeError(`repeat count must be a finite non-negative number, got ${n}`); return Math.floor(c) }\n\"-\".repeat(safeCount(n))","typeGuard":"const isValidCount = (v) => typeof v === \"number\" && Number.isFinite(v) && v >= 0","tryCatchPattern":"try { out = s.repeat(n) } catch (e) { if (String(e.message).includes(\"finite non-negative\")) { n = Math.max(0, Math.floor(Number(n) || 0)); out = s.repeat(n) } else throw e }","preventionTips":["Clamp computed counts with Math.max(0, ...)","Verify subtraction-based counts for inverted bounds","Check for NaN when the count comes from parsing","Floor fractional counts before repeating"],"tags":["codemode","interpreter","range-error","string-methods","argument-validation"],"backgroundTag":"invalid-numeric-range","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}