{"record":{"id":"c03f1157a6c1e197","repo":"vitessio/vitess","slug":"requested-limit-is-out-of-range-v","errorCode":null,"errorMessage":"requested limit is out of range: %v","messagePattern":"requested limit is out of range: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtgate/engine/limit.go","lineNumber":227,"sourceCode":"\tif expr == nil {\n\t\treturn 0, nil\n\t}\n\tevalResult, err := env.Evaluate(expr)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tvalue := evalResult.Value(vcursor.ConnCollation())\n\tif value.IsNull() {\n\t\treturn 0, nil\n\t}\n\n\tif !value.IsIntegral() {\n\t\treturn 0, sqltypes.ErrIncompatibleTypeCast\n\t}\n\n\tcount, err := strconv.Atoi(value.RawStr())\n\tif err != nil || count < 0 {\n\t\treturn 0, fmt.Errorf(\"requested limit is out of range: %v\", value.RawStr())\n\t}\n\treturn count, nil\n}\n\nfunc (l *Limit) description() PrimitiveDescription {\n\tother := map[string]any{}\n\n\tif l.Count != nil {\n\t\tother[\"Count\"] = sqlparser.String(l.Count)\n\t}\n\tif l.Offset != nil {\n\t\tother[\"Offset\"] = sqlparser.String(l.Offset)\n\t}\n\tif l.RequireCompleteInput {\n\t\tother[\"RequireCompleteInput\"] = true\n\t}\n\n\treturn PrimitiveDescription{","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtgate/engine/limit.go#L209-L245","documentation":"This error is thrown by Limit.getIntFrom when the LIMIT/OFFSET value cannot be converted to a valid count. The value must be a positive integral; a non-numeric string, a number too large for an int, or a negative value all fail. Vitess rejects these instead of passing an invalid limit down to MySQL.","triggerScenarios":"Executing a query whose LIMIT or OFFSET binds to a value that is not a valid non-negative integer, e.g. LIMIT 'abc', LIMIT -1, or a bind variable that resolves to a negative or overflow-sized number.","commonSituations":"Application computes a page size from user input without validating it (negative page or page*size overflow); ORM serializes limit as a string with sign or whitespace; int overflow on 32-bit builds with very large limits.","solutions":["Validate the limit/offset is a non-negative integer before sending the query.","Check the bind variable supplying the LIMIT value for sign, type, and stray characters.","Clamp or cap extremely large values to a sane maximum before executing."],"exampleFix":"// before\npageSize := req.PageSize // may be -1 or \"abc\"\nquery := \"SELECT ... LIMIT ?\"\n// after\nif req.PageSize < 0 || req.PageSize > maxPageSize {\n    return fmt.Errorf(\"invalid page size %d\", req.PageSize)\n}\nquery := \"SELECT ... LIMIT ?\"","handlingStrategy":"validation","validationCode":"n, err := strconv.Atoi(limitStr)\nif err != nil || n < 0 {\n    return fmt.Errorf(\"limit must be a non-negative integer, got %q\", limitStr)\n}","typeGuard":"func isValidLimit(s string) bool {\n    n, err := strconv.Atoi(s)\n    return err == nil && n >= 0\n}","tryCatchPattern":null,"preventionTips":["Always validate page size/offset against user input before building the query.","Clamp limits to an application-defined maximum.","Avoid casting user strings to limits without an explicit integer parse."],"tags":["vtgate","limit","input-validation","sql"],"backgroundTag":"invalid-limit-offset-value","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}