{"record":{"id":"dd3b8ff1b188b494","repo":"apache/flink","slug":"offset-d-numbytes-d-address-d","errorCode":null,"errorMessage":"offset=%d, numBytes=%d, address=%d","messagePattern":"offset=(.+?), numBytes=(.+?), address=(.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java","lineNumber":1497,"sourceCode":"        }\n        UNSAFE.copyMemory(this.heapMemory, thisPointer, target, targetPointer, numBytes);\n    }\n\n    /**\n     * Bulk copy method. Copies {@code numBytes} bytes from source unsafe object and pointer. NOTE:\n     * This is an unsafe method, no check here, please be careful.\n     *\n     * @param offset The position where the bytes are started to be write in this memory segment.\n     * @param source The unsafe memory to copy the bytes from.\n     * @param sourcePointer The position in the source unsafe memory to copy the chunk from.\n     * @param numBytes The number of bytes to copy.\n     * @throws IndexOutOfBoundsException If this segment can not contain the given number of bytes\n     *     (starting from offset).\n     */\n    public void copyFromUnsafe(int offset, Object source, int sourcePointer, int numBytes) {\n        final long thisPointer = this.address + offset;\n        if (thisPointer + numBytes > addressLimit) {\n            throw new IndexOutOfBoundsException(\n                    String.format(\n                            \"offset=%d, numBytes=%d, address=%d\", offset, numBytes, this.address));\n        }\n        UNSAFE.copyMemory(source, sourcePointer, this.heapMemory, thisPointer, numBytes);\n    }\n\n    // -------------------------------------------------------------------------\n    //                      Comparisons & Swapping\n    // -------------------------------------------------------------------------\n\n    /**\n     * Compares two memory segment regions.\n     *\n     * @param seg2 Segment to compare this segment with\n     * @param offset1 Offset of this segment to start comparing\n     * @param offset2 Offset of seg2 to start comparing\n     * @param len Length of the compared memory region\n     * @return 0 if equal, -1 if seg1 &lt; seg2, 1 otherwise","sourceCodeStart":1479,"sourceCodeEnd":1515,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java#L1479-L1515","documentation":"copyFromUnsafe(offset, source, sourcePointer, numBytes) copies numBytes bytes from arbitrary unsafe memory into the segment at offset. It is an explicitly unchecked fast path with a single destination guard: if address + offset + numBytes exceeds addressLimit it throws IndexOutOfBoundsException formatted as 'offset=%d, numBytes=%d, address=%d'. Source bounds are not validated at all.","triggerScenarios":"Calling copyFromUnsafe with an offset near the end of the segment such that offset + numBytes overflows the segment size, e.g. writing a record larger than the space left in the current segment instead of rolling over to the next one.","commonSituations":"Record-building loops (normalized key / sort buffers) that forget to request the next segment when the remaining bytes are insufficient; numBytes computed from a serialized length field larger than expected; miscomputed source pointers corrupting downstream size math.","solutions":["Before the call, validate offset >= 0 && offset + numBytes <= segment.size() and move to the next segment when it does not fit.","If numBytes derives from a length prefix, validate it against the record/segment budget before copying.","Decode the formatted message: offset + numBytes vs segment size tells you exactly by how much the write overshot; use it to size the rollover logic."],"exampleFix":"// before\nsegment.copyFromUnsafe(offset, source, srcPtr, numBytes); // IndexOutOfBoundsException\n\n// after\nif (offset + numBytes > segment.size()) {\n    throw new IndexOutOfBoundsException(\"record does not fit in current segment\");\n}\nsegment.copyFromUnsafe(offset, source, srcPtr, numBytes);","handlingStrategy":"validation","validationCode":"if (offset < 0 || numBytes < 0 || offset + numBytes > segment.size()) {\n    throw new IndexOutOfBoundsException(\n            \"offset=\" + offset + \", numBytes=\" + numBytes + \", size=\" + segment.size());\n}\nsegment.copyFromUnsafe(offset, source, srcPtr, numBytes);","typeGuard":null,"tryCatchPattern":"try {\n    segment.copyFromUnsafe(offset, source, srcPtr, numBytes);\n} catch (IndexOutOfBoundsException e) {\n    // message prints offset/numBytes/address; use them to fix rollover math\n    throw e;\n}","preventionTips":["copyFromUnsafe validates only the destination — validate source size yourself.","In multi-segment writers, check remaining space per segment before every copy."],"tags":["memory","unsafe","flink-core","bounds-check"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}