{"record":{"id":"4dfef9d239df3bf1","repo":"apache/hadoop","slug":"bad-value-buffer-offset-length-combination","errorCode":null,"errorMessage":"Bad value buffer offset-length combination.","messagePattern":"Bad value buffer offset-length combination\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/file/tfile/TFile.java","lineNumber":401,"sourceCode":"     * @param voff\n     *          offset in value buffer.\n     * @param vlen\n     *          length of value.\n     * @throws IOException\n     *           Upon IO errors.\n     *           <p>\n     *           If an exception is thrown, the TFile will be in an inconsistent\n     *           state. The only legitimate call after that would be close\n     */\n    public void append(byte[] key, int koff, int klen, byte[] value, int voff,\n        int vlen) throws IOException {\n      if ((koff | klen | (koff + klen) | (key.length - (koff + klen))) < 0) {\n        throw new IndexOutOfBoundsException(\n            \"Bad key buffer offset-length combination.\");\n      }\n\n      if ((voff | vlen | (voff + vlen) | (value.length - (voff + vlen))) < 0) {\n        throw new IndexOutOfBoundsException(\n            \"Bad value buffer offset-length combination.\");\n      }\n\n      try {\n        DataOutputStream dosKey = prepareAppendKey(klen);\n        try {\n          ++errorCount;\n          dosKey.write(key, koff, klen);\n          --errorCount;\n        } finally {\n          dosKey.close();\n        }\n\n        DataOutputStream dosValue = prepareAppendValue(vlen);\n        try {\n          ++errorCount;\n          dosValue.write(value, voff, vlen);\n          --errorCount;","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/file/tfile/TFile.java#L383-L419","documentation":"Thrown by TFile.Writer.append(byte[] key, int koff, int klen, byte[] value, int voff, int vlen) when the value offset/length pair is invalid: voff or vlen negative, voff+vlen overflows int, or voff+vlen exceeds value.length. It is the value-side twin of the key check and fires as IndexOutOfBoundsException before any data is written, leaving the writer untouched by the bad call.","triggerScenarios":"Passing a value length derived from a remaining-bytes computation that went negative, a value offset past the end of the buffer, or lengths from an untrusted/external source without validation.","commonSituations":"Parsing records from a byte stream where the value length field is corrupt or truncated; slicing buffers with end-exclusive vs end-inclusive confusion; reusing one scratch buffer with offsets that drift after variable-length records.","solutions":["Validate voff >= 0, vlen >= 0, and voff + vlen <= value.length (and the key pair) before calling append","Use append(byte[] key, byte[] value) when the full arrays form the record","Validate external length fields at ingestion time and reject malformed records explicitly"],"exampleFix":"// before\nwriter.append(key, 0, key.length, value, valOff, valLen);\n\n// after\nif (valOff < 0 || valLen < 0 || valOff + valLen > value.length) {\n  throw new IllegalArgumentException(\"Bad value offset/length: \" + valOff + \"/\" + valLen);\n}\nwriter.append(key, 0, key.length, value, valOff, valLen);","handlingStrategy":"validation","validationCode":"static void checkValueBounds(byte[] value, int voff, int vlen) {\n  if (value == null || voff < 0 || vlen < 0 || voff + vlen < 0 || voff + vlen > value.length) {\n    throw new IllegalArgumentException(\"bad value bounds: off=\" + voff + \" len=\" + vlen);\n  }\n}\ncheckValueBounds(value, voff, vlen);\nwriter.append(key, koff, klen, value, voff, vlen);","typeGuard":null,"tryCatchPattern":"catch (IndexOutOfBoundsException e) {\n  // pre-write check failed; nothing appended: skip or repair the malformed record\n}","preventionTips":["Validate both key and value pairs in one helper before every offset-based append","Reject malformed external records explicitly instead of letting array bounds surface downstream","Use end-exclusive slicing consistently (off + len <= buffer.length)"],"tags":["hadoop","tfile","bounds-check","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}