{"record":{"id":"99e94555bc46e309","repo":"apache/cassandra","slug":"invalid-limit-value","errorCode":null,"errorMessage":"Invalid limit value","messagePattern":"Invalid limit value","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/SelectStatement.java","lineNumber":1055,"sourceCode":"    private int getLimit(Term limit, QueryOptions options)\n    {\n        int userLimit = DataLimits.NO_LIMIT;\n\n        if (limit != null)\n        {\n            ByteBuffer b = checkNotNull(limit.bindAndGet(options), \"Invalid null value of limit\");\n            // treat UNSET limit value as 'unlimited'\n            if (b != UNSET_BYTE_BUFFER)\n            {\n                try\n                {\n                    Int32Type.instance.validate(b);\n                    userLimit = Int32Type.instance.compose(b);\n                    checkTrue(userLimit > 0, \"LIMIT must be strictly positive\");\n                }\n                catch (MarshalException e)\n                {\n                    throw new InvalidRequestException(\"Invalid limit value\");\n                }\n            }\n        }\n        return userLimit;\n    }\n\n    private NavigableSet<Clustering<?>> getRequestedRows(QueryOptions options, ClientState state) throws InvalidRequestException\n    {\n        // Note: getRequestedColumns don't handle static columns, but due to CASSANDRA-5762\n        // we always do a slice for CQL3 tables, so it's ok to ignore them here\n        assert !restrictions.isColumnRange();\n        return restrictions.getClusteringColumns(options, state);\n    }\n\n    /**\n     * May be used by custom QueryHandler implementations\n     */\n    public RowFilter getRowFilter(QueryOptions options, ClientState state) throws InvalidRequestException","sourceCodeStart":1037,"sourceCodeEnd":1073,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java#L1037-L1073","documentation":"SelectStatement builds the effective LIMIT from the bound variables; when a LIMIT provided via a bind marker fails to unmarshal as an Int32 (Int32Type.validate throws MarshalException), it is rejected with InvalidRequestException 'Invalid limit value'. It means the byte payload bound to the LIMIT variable is not a valid 4-byte int, or (in programmatic use) the value's serialization is malformed.","triggerScenarios":"Binding a driver value of the wrong type/format to the LIMIT ? placeholder (e.g. a string '10', a long/short with wrong encoding, or raw bytes that are not 4-byte big-endian ints) in a prepared statement.","commonSituations":"Low-level clients or custom drivers binding raw byte buffers for LIMIT; ORM/tooling that interpolates LIMIT as a string; ported code that previously used string limits.","solutions":["Bind LIMIT ? as a proper int32 value using the driver's int type","Do not pass the limit as a string; convert client-side before binding","If writing raw bytes, ensure exactly 4 bytes big-endian signed int > 0","Alternatively inline a literal LIMIT n instead of a bind marker"],"exampleFix":"// before\nstmt.bind(\"10\"); // LIMIT ? bound as string\n// after\nstmt.bind(10); // bind as int","handlingStrategy":"type-guard","validationCode":"if (typeof limit !== 'number' || !Number.isInteger(limit) || limit <= 0) throw new Error('LIMIT must be a positive integer before binding');","typeGuard":"function isValidLimit(v) { return Number.isInteger(v) && v > 0 && v <= 2147483647; }","tryCatchPattern":"try { session.execute(stmt, params); } catch (e) { if (e.message === 'Invalid limit value') coerceLimitParamToInt(params); else throw e; }","preventionTips":["Bind LIMIT placeholders with the driver's int type, never strings or raw buffers","Validate limit values are positive 32-bit ints at the API boundary","Prefer literal LIMIT n when the value is known at query-build time"],"tags":["cql","limit","binding","type-mismatch"],"backgroundTag":"invalid-argument-value","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"}