{"record":{"id":"4bdf76104ef96603","repo":"karatelabs/karate","slug":"invalid-count-value","errorCode":null,"errorMessage":"Invalid count value","messagePattern":"Invalid count value","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsStringPrototype.java","lineNumber":357,"sourceCode":"        int padLength = targetLength - s.length();\n        StringBuilder sb = new StringBuilder();\n        for (int i = 0; i < padLength; i++) {\n            sb.append(padString.charAt(i % padString.length()));\n        }\n        sb.append(s);\n        return sb.toString();\n    }\n\n    private Object repeat(Context context, Object[] args) {\n        String s = thisString(context, \"repeat\");\n        // Spec: ToIntegerOrInfinity, then RangeError if < 0 or +Infinity.\n        // We only carry double precision through argInt; check the original\n        // numeric for the -0 / +Infinity edge before clamping.\n        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) return \"\";\n        double d = Terms.objectToNumber(args[0]).doubleValue();\n        if (Double.isNaN(d)) return \"\";\n        if (d < 0 || Double.isInfinite(d)) {\n            throw JsErrorException.rangeError(\"Invalid count value\");\n        }\n        int count = (int) d;\n        return s.repeat(count);\n    }\n\n    private Object slice(Context context, Object[] args) {\n        String s = thisString(context, \"slice\");\n        int beginIndex = argInt(args, 0, 0);\n        int endIndex = (args.length > 1 && args[1] != Terms.UNDEFINED) ? argInt(args, 1, s.length()) : s.length();\n        // handle negative indices\n        if (beginIndex < 0) beginIndex = Math.max(s.length() + beginIndex, 0);\n        if (endIndex < 0) endIndex = Math.max(s.length() + endIndex, 0);\n        // ensure proper range\n        beginIndex = Math.min(beginIndex, s.length());\n        endIndex = Math.min(endIndex, s.length());\n        if (beginIndex >= endIndex) return \"\";\n        return s.substring(beginIndex, endIndex);\n    }","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsStringPrototype.java#L339-L375","documentation":"This RangeError is thrown by String.prototype.repeat when the count argument is negative or +Infinity. A NaN count yields the empty string per spec, but negative and infinite counts have no valid result, so the engine raises RangeError before clamping to an integer.","triggerScenarios":"Calling 'x'.repeat(-1) or 'x'.repeat(Infinity); passing a computed count from division (e.g. total/size that is negative or infinite); counts derived from NaN-free but unbounded math.","commonSituations":"Padding logic where the count calculation divides by zero (→ Infinity); user input used directly as repeat count; integer underflow producing negative values; porting code where counts were previously truncated.","solutions":["Clamp the count before calling: count = Math.max(0, Math.min(count, maxLen/str.length))","Check Math.isFinite(count) && count >= 0 before repeating","Cap counts derived from division to avoid Infinity (guard the divisor)"],"exampleFix":"// before\nconst pad = ' '.repeat(width - s.length); // RangeError if width < s.length\n// after\nconst pad = ' '.repeat(Math.max(0, width - s.length));","handlingStrategy":"validation","validationCode":"const safeRepeat = (s, n) => (Number.isFinite(n) && n >= 0 ? s.repeat(Math.floor(n)) : '');","typeGuard":"const isValidCount = (n) => Number.isFinite(n) && n >= 0;","tryCatchPattern":"try { return s.repeat(count); } catch (e) { if (e.name === 'RangeError') return ''; throw e; }","preventionTips":["Clamp counts: Math.max(0, Math.floor(count)) before repeating","Guard divisions used to derive counts (avoid divide-by-zero producing Infinity)","Bound repeat counts against expected output size to avoid memory blowups too"],"tags":["javascript","string","range-error"],"backgroundTag":"value-out-of-range","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}