{"record":{"id":"eaa9db0ae8efd63e","repo":"karatelabs/karate","slug":"invalid-array-length","errorCode":null,"errorMessage":"Invalid array length","messagePattern":"Invalid array length","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsArray.java","lineNumber":1044,"sourceCode":"    @Override\n    public Object call(Context context, Object[] args) {\n        return create(args);\n    }\n\n    /**\n     * ES6: Both Array() and new Array() return an Array object.\n     * Array(n) creates a sparse array of length n (HOLE-filled, per spec —\n     * {@code new Array(3).hasOwnProperty(0) === false}); Array(a,b,c) creates\n     * [a,b,c]. Throws RangeError when the single-numeric form's argument is\n     * not a valid Uint32 — covers {@code new Array(-1)} /\n     * {@code new Array(4294967296)} / {@code new Array(1.5)}.\n     */\n    static JsArray create(Object[] args) {\n        if (args.length == 1 && args[0] instanceof Number n) {\n            double d = n.doubleValue();\n            if (Double.isNaN(d) || Double.isInfinite(d) || d < 0\n                    || d > 4294967295.0 || d != Math.floor(d)) {\n                throw JsErrorException.rangeError(\"Invalid array length\");\n            }\n            if (d > Integer.MAX_VALUE) {\n                throw JsErrorException.rangeError(\"Invalid array length\");\n            }\n            int count = (int) d;\n            List<Object> list = new ArrayList<>(count);\n            for (int i = 0; i < count; i++) {\n                list.add(HOLE);\n            }\n            return new JsArray(list);\n        }\n        return new JsArray(new ArrayList<>(Arrays.asList(args)));\n    }\n\n    // Use identity-based hashCode/equals to avoid infinite recursion\n    // when arrays contain objects with circular references\n    @Override\n    public int hashCode() {","sourceCodeStart":1026,"sourceCodeEnd":1062,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsArray.java#L1026-L1062","documentation":"When the Array constructor is called with a single numeric argument (Array(n)), the number must be a valid, non-negative, integral, unsigned-32-bit array length. NaN, Infinity, negative, non-integer, or values above 4294967295 produce this RangeError, mirroring the ECMAScript spec.","triggerScenarios":"new Array(-1), new Array(2.5), new Array(NaN), new Array(Infinity), new Array(4294967296).","commonSituations":"Computing a length from a parsed value or variable that ends up negative/NaN; off-by-one or unit-conversion arithmetic producing fractional lengths; porting code that relied on coerced lengths.","solutions":["Validate the length before constructing: ensure it is a non-negative integer <= 4294967295","Use Math.floor/Math.trunc on computed lengths, and guard against NaN","If you want an array with that element, pass it as a second argument or use literal syntax instead"],"exampleFix":"// before\nvar n = getSize(); var arr = new Array(n); // RangeError if n invalid\n// after\nvar n = Math.trunc(getSize()); if (n >= 0 && n <= 4294967295) { var arr = new Array(n); }","handlingStrategy":"validation","validationCode":"function isValidArrayLength(n) { return typeof n === 'number' && Number.isInteger(n) && n >= 0 && n <= 4294967295; }","typeGuard":"function isValidArrayLength(n) { return typeof n === 'number' && Number.isInteger(n) && n >= 0 && n <= 4294967295; }","tryCatchPattern":"try { var arr = new Array(n); } catch (e) { if (String(e).indexOf('Invalid array length') !== -1) { arr = []; } else { throw e; } }","preventionTips":["Validate computed lengths before passing to new Array(n)","Use Array.of or literals unless you truly need pre-allocation","Guard against NaN/negative results from arithmetic or parsing"],"tags":["javascript","array","range-error","validation"],"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-16T19:17:19.609Z"}