{"record":{"id":"feab6fb11328d29c","repo":"apache/cassandra","slug":"list-index-d-out-of-bound-list-has-size-d","errorCode":null,"errorMessage":"List index %d out of bound, list has size %d","messagePattern":"List index (.+?) out of bound, list has size (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Lists.java","lineNumber":374,"sourceCode":"\n            Guardrails.readBeforeWriteListOperationsEnabled\n            .ensureEnabled(\"Setting of list items by index requiring read before write\", builder.clientState);\n\n            ByteBuffer index = idx.bindAndGet(builder);\n            ByteBuffer value = t.bindAndGet(builder);\n\n            if (index == null)\n                throw new InvalidRequestException(\"Invalid null value for list index\");\n            if (index == ByteBufferUtil.UNSET_BYTE_BUFFER)\n                throw new InvalidRequestException(\"Invalid unset value for list index\");\n\n            Row existingRow = builder.getPrefetchedRow(partitionKey, builder.currentClustering());\n            int existingSize = existingSize(existingRow, column);\n            int idx = ByteBufferUtil.toInt(index);\n            if (existingSize == 0)\n                throw new InvalidRequestException(\"Attempted to set an element on a list which is null\");\n            if (idx < 0 || idx >= existingSize)\n                throw new InvalidRequestException(String.format(\"List index %d out of bound, list has size %d\", idx, existingSize));\n\n            CellPath elementPath = existingRow.getComplexColumnData(column).getCellByIndex(idx).path();\n            if (value == null)\n                builder.addTombstone(column, elementPath);\n            else if (value != ByteBufferUtil.UNSET_BYTE_BUFFER)\n                builder.addCell(column, elementPath, value);\n        }\n    }\n\n    public static class Appender extends Operation\n    {\n        public Appender(ColumnMetadata column, Term t)\n        {\n            super(column, t);\n        }\n\n        public void execute(DecoratedKey partitionKey, RowUpdateBuilder builder) throws InvalidRequestException\n        {","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Lists.java#L356-L392","documentation":"Thrown when the index used in a list element assignment is outside the bounds of the existing list (negative, or >= current size). Lists in Cassandra are cell-indexed by their existing element paths; you can only overwrite existing positions, not grow via index.","triggerScenarios":"`UPDATE t SET l[5] = 'v'` when l has 2 elements; any negative index; index computed from stale client-side data after concurrent writes shrunk the list.","commonSituations":"Off-by-one errors (using size instead of size-1); stale UI index after list mutation; assuming CQL lists behave like zero-padded arrays.","solutions":["Fetch the list first and validate 0 <= index < size before the indexed update","Use list append (l = l + [...]) to grow, or rewrite the whole list, instead of out-of-range indexes","Re-read the list after concurrent mutations before computing the index"],"exampleFix":"// before\nint idx = list.size(); // off-by-one, out of bounds\nUPDATE t SET l[idx] = 'v';\n// after\nint idx = list.size() - 1;\nif (idx >= 0) UPDATE t SET l[idx] = 'v';","handlingStrategy":"validation","validationCode":"List<String> l = row.getList(\"l\", String.class); if (idx < 0 || idx >= l.size()) throw new IndexOutOfBoundsException(\"idx=\" + idx + \" size=\" + l.size());","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"List index\") && e.getMessage().contains(\"out of bound\")) { /* re-read list and recompute index */ } else throw e; }","preventionTips":["Read the list immediately before indexed writes","Use size-1 not size for last element","Handle concurrency: lists can shrink between read and write"],"tags":["cql","list","index-out-of-bounds","bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}