{"record":{"id":"e1a1198646885eaf","repo":"apache/hadoop","slug":"bad-key-buffer-offset-length-combination","errorCode":null,"errorMessage":"Bad key buffer offset-length combination.","messagePattern":"Bad key 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":396,"sourceCode":"     *          offset in key buffer.\n     * @param klen\n     *          length of key.\n     * @param value\n     *          buffer for value.\n     * @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","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/file/tfile/TFile.java#L378-L414","documentation":"Thrown by TFile.Writer.append(byte[] key, int koff, int klen, byte[] value, int voff, int vlen) when the key offset/length pair is invalid: koff or klen negative, koff+klen overflows int, or koff+klen exceeds key.length. The check is the classic bitwise trick (koff | klen | (koff+klen) | (key.length-(koff+klen))) < 0. It fires as IndexOutOfBoundsException before any bytes are written, so the TFile is not corrupted by the bad call.","triggerScenarios":"Passing lengths parsed from external data (e.g. unsigned fields read as negative ints), an offset computed as key.length - klen with wrong signs, or klen larger than the remaining buffer from koff.","commonSituations":"Deserializing lengths from network/file input without range checks; copying code from ByteBuffer-based loops into array-based append calls; off-by-one errors where klen includes a trailing byte; arithmetic that silently overflows int for large buffers.","solutions":["Validate koff >= 0, klen >= 0, and koff + klen <= key.length before calling append","Prefer the simple overload append(byte[] key, byte[] value) when the whole arrays are the record","Where lengths come from external input, reject or clamp them at the parse boundary with an explicit error message"],"exampleFix":"// before\nwriter.append(keyBuf, keyOff, keyLen, valBuf, valOff, valLen);\n\n// after\nif (keyOff < 0 || keyLen < 0 || keyOff + keyLen > keyBuf.length\n    || valOff < 0 || valLen < 0 || valOff + valLen > valBuf.length) {\n  throw new IllegalArgumentException(\"Bad offset/length\");\n}\nwriter.append(keyBuf, keyOff, keyLen, valBuf, valOff, valLen);","handlingStrategy":"validation","validationCode":"static void checkKeyBounds(byte[] key, int koff, int klen) {\n  if (key == null || koff < 0 || klen < 0 || koff + klen < 0 || koff + klen > key.length) {\n    throw new IllegalArgumentException(\"bad key bounds: off=\" + koff + \" len=\" + klen + \" buf=\" + (key == null ? -1 : key.length));\n  }\n}\ncheckKeyBounds(key, koff, klen);\nwriter.append(key, koff, klen, value, voff, vlen);","typeGuard":null,"tryCatchPattern":"catch (IndexOutOfBoundsException e) {\n  // nothing was written; fix or drop the record and continue with the next one\n}","preventionTips":["Validate offset/length pairs at the parse boundary where external lengths enter the system","Prefer whole-array append(key, value) when slicing is unnecessary","Watch for int overflow when computing koff + klen for large buffers"],"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-23T01:17:44.959Z"}