{"record":{"id":"68cd3e361f2fc6ca","repo":"karatelabs/karate","slug":"index-index-size-buffer-length","errorCode":null,"errorMessage":"Index: <index>, Size: <buffer.length>","messagePattern":"Index: <index>, Size: <buffer\\.length>","errorType":"exception","errorClass":"java.lang.IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsUint8Array.java","lineNumber":174,"sourceCode":"\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    public <T> T[] toArray(T[] a) {\n        int size = buffer.length;\n        T[] result = a.length >= size ? a : (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);\n        for (int i = 0; i < size; i++) {\n            result[i] = (T) (Integer) (buffer[i] & 0xFF);\n        }\n        if (a.length > size) {\n            result[size] = null;\n        }\n        return result;\n    }\n\n    @Override\n    public Object get(int index) {\n        if (index < 0 || index >= buffer.length) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Size: \" + buffer.length);\n        }\n        return buffer[index] & 0xFF;\n    }\n\n    @Override\n    public Object set(int index, Object element) {\n        if (index < 0 || index >= buffer.length) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Size: \" + buffer.length);\n        }\n        int previous = buffer[index] & 0xFF;\n        if (element instanceof Number n) {\n            buffer[index] = (byte) (n.intValue() & 0xFF);\n        }\n        return previous;\n    }\n\n    @Override\n    public int indexOf(Object o) {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsUint8Array.java#L156-L192","documentation":"JsUint8Array.get(index) throws IndexOutOfBoundsException with the message 'Index: <index>, Size: <buffer.length>' when the requested index is negative or >= the underlying byte buffer length. This mirrors Java's ArrayList bounds message so it is familiar to Java developers. It means JS code read a byte position outside the array's allocated size.","triggerScenarios":"Reading u8[i] where i < 0 or i >= u8.length, e.g. u8[u8.length] (one past the end) or u8[-1].","commonSituations":"Off-by-one loops (`i <= arr.length`); assuming JS arrays auto-extend like plain arrays; using an index computed from another array of a different size.","solutions":["Check the index against the array's length before reading.","Use for (var i = 0; i < arr.length; i++) — not <=.","If out-of-range reads should be tolerated, wrap the access in try/catch or use a bounds-checking helper."],"exampleFix":"// before\nvar b = u8[i];\n// after\nif (i >= 0 && i < u8.length) { var b = u8[i]; }","handlingStrategy":"validation","validationCode":"function canRead(arr, i) { return i >= 0 && i < arr.length; }\nif (!canRead(u8, i)) throw new Error('byte index out of range: ' + i);","typeGuard":"function inBounds(arr, i) { return Number.isInteger(i) && i >= 0 && i < arr.length; }","tryCatchPattern":"try { b = u8[i]; } catch (e) { if (karate.match(e, '#string').length > 0 && e.indexOf('IndexOutOfBoundsException') >= 0) { b = 0; } else { throw e; } }","preventionTips":["Use strict '<' in loops over typed arrays, never '<='.","Never index a typed array with -1 or arr.length.","Compute indices from the same array's length you are indexing."],"tags":["javascript","typed-array","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","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"}