{"record":{"id":"88a55b96963ec3d7","repo":"netty/netty","slug":"cindex-d-expected-0-numcomponents-d","errorCode":null,"errorMessage":"cIndex: %d (expected: >= 0 && <= numComponents(%d))","messagePattern":"cIndex: (.+?) \\(expected: >= 0 && <= numComponents\\((.+?)\\)\\)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java","lineNumber":578,"sourceCode":"    }\n\n    /**\n     * This should only be called as last operation from a method as this may adjust the underlying\n     * array of components and so affect the index etc.\n     */\n    private void consolidateIfNeeded() {\n        // Consolidate if the number of components will exceed the allowed maximum by the current\n        // operation.\n        int size = componentCount;\n        if (size > maxNumComponents) {\n            consolidate0(0, size);\n        }\n    }\n\n    private void checkComponentIndex(int cIndex) {\n        ensureAccessible();\n        if (cIndex < 0 || cIndex > componentCount) {\n            throw new IndexOutOfBoundsException(String.format(\n                    \"cIndex: %d (expected: >= 0 && <= numComponents(%d))\",\n                    cIndex, componentCount));\n        }\n    }\n\n    private void checkComponentIndex(int cIndex, int numComponents) {\n        ensureAccessible();\n        if (cIndex < 0 || cIndex + numComponents > componentCount) {\n            throw new IndexOutOfBoundsException(String.format(\n                    \"cIndex: %d, numComponents: %d \" +\n                    \"(expected: cIndex >= 0 && cIndex + numComponents <= totalNumComponents(%d))\",\n                    cIndex, numComponents, componentCount));\n        }\n    }\n\n    private void updateComponentOffsets(int cIndex) {\n        int size = componentCount;\n        if (size <= cIndex) {","sourceCodeStart":560,"sourceCodeEnd":596,"githubUrl":"https://github.com/netty/netty/blob/70040aacae241e9ba371e758f5b86e470bd77ad1/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java#L560-L596","documentation":"Thrown by CompositeByteBuf.checkComponentIndex(int cIndex) when cIndex is negative or greater than componentCount. It is an IndexOutOfBoundsException guarding insertion/retrieval by component index. Valid insertion points are 0..componentCount inclusive (appending at componentCount is allowed).","triggerScenarios":"Calling addComponent(increaseWriterIndex, cIndex, buffer) or any cIndex-based API with cIndex < 0 or cIndex > componentCount, e.g. using an offset past the end or a stale count after concurrent removal.","commonSituations":"Storing componentCount before adding components and reusing it; treating component indices as 0-based past-the-end; concurrent pipeline threads mutating one composite.","solutions":["Query the live count right before use: int n = comp.numComponents(); add at n to append.","Prefer the append overloads addComponent(buffer) / addComponents(buffer...) which handle indexing for you.","Synchronize external access to a shared composite or give each thread its own."],"exampleFix":"// before\ncomp.addComponent(true, cachedCount, buf); // cachedCount may be stale\n\n// after\ncomp.addComponent(true, comp.numComponents(), buf); // safe append","handlingStrategy":"validation","validationCode":"static void addAt(CompositeByteBuf comp, int cIndex, ByteBuf buf) {\n    if (cIndex < 0 || cIndex > comp.numComponents()) {\n        cIndex = comp.numComponents(); // append\n    }\n    comp.addComponent(true, cIndex, buf);\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Read numComponents() immediately before indexing; do not cache it.","Prefer append overloads that need no index.","Keep a composite thread-confined to avoid stale counts."],"tags":["netty","buffer","composite","index-out-of-bounds","components"],"backgroundTag":null,"analyzedSha":"70040aacae241e9ba371e758f5b86e470bd77ad1","analyzedAt":"2026-08-14T01:29:15.551Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}