{"record":{"id":"7d1d7589aaf39942","repo":"valyala/fasthttp","slug":"the-start-position-of-byte-range-cannot-exceed-the","errorCode":null,"errorMessage":"the start position of byte range cannot exceed the end position. byte range %q","messagePattern":"the start position of byte range cannot exceed the end position\\. byte range %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"fs.go","lineNumber":1529,"sourceCode":"\t\treturn 0, 0, err\n\t}\n\tif startPos >= contentLength {\n\t\treturn 0, 0, fmt.Errorf(\"the start position of byte range cannot exceed %d. byte range %q\", contentLength-1, byteRange)\n\t}\n\n\tb = b[n+1:]\n\tif len(b) == 0 {\n\t\treturn startPos, contentLength - 1, nil\n\t}\n\n\tif endPos, err = ParseUint(b); err != nil {\n\t\treturn 0, 0, err\n\t}\n\tif endPos >= contentLength {\n\t\tendPos = contentLength - 1\n\t}\n\tif endPos < startPos {\n\t\treturn 0, 0, fmt.Errorf(\"the start position of byte range cannot exceed the end position. byte range %q\", byteRange)\n\t}\n\treturn startPos, endPos, nil\n}\n\nfunc (h *fsHandler) openIndexFile(ctx *RequestCtx, dirPath string, mustCompress bool, fileEncoding string) (*fsFile, error) {\n\tfor _, indexName := range h.indexNames {\n\t\tindexFilePath := indexName\n\t\tif dirPath != \"\" {\n\t\t\tindexFilePath = dirPath + \"/\" + indexName\n\t\t}\n\n\t\tff, err := h.openFSFile(indexFilePath, mustCompress, fileEncoding)\n\t\tif err == nil {\n\t\t\treturn ff, nil\n\t\t}\n\t\tif mustCompress && err == errNoCreatePermission {\n\t\t\tctx.Logger().Printf(\"insufficient permissions for saving compressed file for %q. Serving uncompressed file. \"+\n\t\t\t\t\"Allow write access to the directory with this file in order to improve fasthttp performance\", indexFilePath)","sourceCodeStart":1511,"sourceCodeEnd":1547,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/fs.go#L1511-L1547","documentation":"After parsing start and end offsets and clamping end to contentLength-1, fasthttp requires end >= start. A range like bytes=500-100 where 500 > the (clamped) end is unsatisfiable and returns this error.","triggerScenarios":"Client sends an inverted range (bytes=100-50); end position exceeded contentLength and was clamped below an already-too-large start (e.g. bytes=900-9999 for a 1000-byte body becomes end=999 < start=900); direct ParseByteRange calls with inverted values.","commonSituations":"Buggy range-splitting code in download managers (chunk boundaries computed wrong); clients reusing offsets across differently sized files; off-by-one when converting 'first byte + length' to inclusive start/end.","solutions":["Fix the client to always send end >= start (inclusive byte positions)","Validate before sending: if start > end, send the full GET or a corrected range","If the end was clamped by server-side contentLength, your start is also stale — re-fetch file size/ETag and restart","For direct calls, check endPos >= startPos before invoking ParseByteRange"],"exampleFix":"// before\nRange: bytes=500-100\n// after\nstart, end = min(start, end), max(start, end)\nRange: bytes=100-500","handlingStrategy":"validation","validationCode":"if start > end {\n    start, end = end, start // or reject the header\n}","typeGuard":"func rangeOrdered(start, end int) bool { return start <= end }","tryCatchPattern":"start, end, err := fs.ParseByteRange(hdr, cl)\nif err != nil && strings.Contains(err.Error(), \"cannot exceed the end position\") {\n    return 416\n}","preventionTips":["Compute end as start + length - 1 and assert start <= end before sending","Clamp end to contentLength-1 client-side so clamping never inverts the range","Unit-test chunk-splitting logic in download managers","Log the offending range to identify the buggy client"],"tags":["http","range-header","validation","fasthttp"],"backgroundTag":"range-start-exceeds-end","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}