{"record":{"id":"a84661dc6db58831","repo":"ipfs/kubo","slug":"cannot-specify-negative-length","errorCode":null,"errorMessage":"cannot specify negative length","messagePattern":"cannot specify negative length","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/cat.go","lineNumber":53,"sourceCode":"\t\tcmds.Int64Option(offsetOptionName, \"o\", \"Byte offset to begin reading from.\"),\n\t\tcmds.Int64Option(lengthOptionName, \"l\", \"Maximum number of bytes to read.\"),\n\t\tcmds.BoolOption(progressOptionName, \"p\", \"Stream progress data. Defaults to true when stderr is a terminal.\"),\n\t},\n\tRun: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {\n\t\tapi, err := cmdenv.GetApi(env, req)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\toffset, _ := req.Options[offsetOptionName].(int64)\n\t\tif offset < 0 {\n\t\t\treturn errors.New(\"cannot specify negative offset\")\n\t\t}\n\n\t\tmax, found := req.Options[lengthOptionName].(int64)\n\n\t\tif max < 0 {\n\t\t\treturn errors.New(\"cannot specify negative length\")\n\t\t}\n\t\tif !found {\n\t\t\tmax = -1\n\t\t}\n\n\t\terr = req.ParseBodyArgs()\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\treaders, length, err := cat(req.Context, api, req.Arguments, int64(offset), int64(max))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t/*\n\t\t\tif err := corerepo.ConditionalGC(req.Context, node, length); err != nil {\n\t\t\t\tre.SetError(err, cmds.ErrNormal)","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/cat.go#L35-L71","documentation":"`ipfs cat` accepts a `--length` option that must be non-negative; a negative value triggers `cannot specify negative length`. Note the check runs before the `found` test, so an explicitly provided negative length is rejected even though an absent length defaults internally to -1 (unlimited).","triggerScenarios":"`ipfs cat <cid> --length -100` or an RPC call passing a negative length option.","commonSituations":"Scripts computing remaining bytes as `total - offset` where offset exceeds total; copying the internal sentinel -1 into the CLI where it is not accepted.","solutions":["Compute remaining = total - offset and clamp to >= 0 before passing --length","Omit --length entirely to read to end of file (do not pass -1)","Validate numeric input before constructing the command"],"exampleFix":"// before\nipfs cat $CID --offset 5000 --length -1\n// after\nipfs cat $CID --offset 5000   # reads to EOF","handlingStrategy":"validation","validationCode":"if length != nil && *length < 0 {\n    return errors.New(\"length must be >= 0; omit it to read to EOF\")\n}","typeGuard":"null","tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"negative length\") {\n    // drop --length and retry to read to EOF\n}","preventionTips":["Omit --length rather than passing sentinels like -1","Clamp remaining-bytes calculations to >= 0","Validate numeric options before building the command","Remember the internal -1 sentinel is not a valid CLI value"],"tags":["cli","cat","argument-validation"],"backgroundTag":"invalid-argument-range","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}