{"record":{"id":"f474109c73cecd21","repo":"apache/flink","slug":"length-cannot-be-negative","errorCode":null,"errorMessage":"Length cannot be negative.","messagePattern":"Length cannot be negative\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/memory/DataInputDeserializer.java","lineNumber":389,"sourceCode":"\n    @Override\n    public void skipBytesToRead(int numBytes) throws IOException {\n        int skippedBytes = skipBytes(numBytes);\n\n        if (skippedBytes < numBytes) {\n            throw new EOFException(\"Could not skip \" + numBytes + \" bytes.\");\n        }\n    }\n\n    @Override\n    public int read(@Nonnull byte[] b, int off, int len) throws IOException {\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.buffer, this.position, b, off, toRead);\n            this.position += toRead;\n\n            return toRead;\n        }\n    }\n","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/DataInputDeserializer.java#L371-L407","documentation":"Thrown by DataInputDeserializer.read(byte[], int off, int len) when len is negative. The method rejects negative read lengths up front with a clear message.","triggerScenarios":"Calling read(b, off, len) with len < 0.","commonSituations":"len derived from a size that underflowed to negative; a remaining-bytes computation that goes below zero; passing a -1 default sentinel.","solutions":["Ensure len >= 0 before calling read(b, off, len).","Treat len == 0 explicitly (read returns 0) rather than passing a computed negative.","Clamp len from external/sourced values: len = Math.max(0, requested)."],"exampleFix":"// before\ninput.read(dst, off, remaining); // remaining may be < 0\n\n// after\nif (remaining <= 0) {\n    return; // nothing to read\n}\ninput.read(dst, off, remaining);","handlingStrategy":"validation","validationCode":"if (len < 0) {\n    throw new IndexOutOfBoundsException(\"len must be >= 0, got \" + len);\n}\ninput.read(dst, off, len);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp externally sourced lengths: len = Math.max(0, requested).","Handle len == 0 explicitly rather than passing a computed negative.","Treat a negative computed length as a bug in upstream size arithmetic."],"tags":["serialization","bounds-check","deserialization","validation"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}