{"record":{"id":"a5fbfea3db3eef34","repo":"apache/flink","slug":"byte-array-does-not-provide-enough-space-to-store-a5fbfe","errorCode":null,"errorMessage":"Byte array does not provide enough space to store requested data.","messagePattern":"Byte array does not provide enough space to store requested data\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/types/Record.java","lineNumber":1587,"sourceCode":"            skipBytes(numBytes);\n        }\n\n        @Override\n        public int read(byte[] b, int off, int len) throws IOException {\n            if (b == null) {\n                throw new NullPointerException(\"Byte array b cannot be null.\");\n            }\n\n            if (off < 0) {\n                throw new IndexOutOfBoundsException(\"Offset cannot be negative.\");\n            }\n\n            if (len < 0) {\n                throw new IndexOutOfBoundsException(\"Length cannot be negative.\");\n            }\n\n            if (b.length - off < len) {\n                throw new IndexOutOfBoundsException(\n                        \"Byte array does not provide enough space to store requested data\" + \".\");\n            }\n\n            if (this.position >= this.end) {\n                return len == 0 ? 0 : -1;\n            } else {\n                int toRead = Math.min(this.end - this.position, len);\n                System.arraycopy(this.memory, this.position, b, off, toRead);\n                this.position += toRead;\n\n                return toRead;\n            }\n        }\n\n        @Override\n        public int read(byte[] b) throws IOException {\n            return read(b, 0, b.length);\n        }","sourceCodeStart":1569,"sourceCodeEnd":1605,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/types/Record.java#L1569-L1605","documentation":"Thrown by the read side of Record's serialization buffer (an InputStream view over the record's memory). It is an IndexOutOfBoundsException raised before any bytes are copied: the caller supplied a target byte array b where the remaining space after the offset (b.length - off) is smaller than the requested read length len. The library validates arguments eagerly instead of letting System.arraycopy fail deeper in.","triggerScenarios":"Calling Record's deserialization input view read(byte[] b, int off, int len) (or an InputStream wrapper over it) with len larger than b.length - off, e.g. read(buf, 10, 100) on a 50-byte buffer; also any off > b.length with a positive len.","commonSituations":"Reusable scratch buffers that were shrunk or allocated with the wrong size between records; off/len swapped by mistake; code that assumes read fills the whole array regardless of remaining capacity.","solutions":["Size the target array to at least off + len before the call","Clamp the request: int n = Math.min(len, b.length - off);","Check the arguments up front and reject/log malformed requests instead of catching the exception"],"exampleFix":"// before\nbyte[] buf = new byte[64];\nin.read(buf, 0, 128); // IndexOutOfBoundsException\n\n// after\nbyte[] buf = new byte[128];\nin.read(buf, 0, 128);","handlingStrategy":"validation","validationCode":"if (b == null) throw new NullPointerException(\"b\");\nif (off < 0 || len < 0 || b.length - off < len) {\n    throw new IllegalArgumentException(\"read range [\" + off + \",\" + (off + len) + \") exceeds buffer \" + b.length);\n}","typeGuard":null,"tryCatchPattern":"catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException(\"Malformed read request\", e); }","preventionTips":["Size buffers to off + len","Min/clamp requested length against remaining capacity","Unit-test boundary sizes 0, 1, and exact buffer length"],"tags":["flink","serialization","record","index-out-of-bounds","argument-validation"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}