{"record":{"id":"36c43167425a47c8","repo":"jstedfast/MailKit","slug":"count-imapfolderfetch","errorCode":null,"errorMessage":"count","messagePattern":"count","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":4190,"sourceCode":"\t\t\tif (index < 0 || index >= Count)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\tif (part == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (part));\n\n\t\t\treturn GetBodyPartAsync (index, part.PartSpecifier, cancellationToken, progress);\n\t\t}\n\n\t\tbool TryQueueGetStreamCommand (UniqueId uid, int offset, int count, CancellationToken cancellationToken, ITransferProgress? progress, [NotNullWhen (true)] out ImapCommand? ic, [NotNullWhen (true)] out FetchStreamContext? ctx)\n\t\t{\n\t\t\tif (!uid.IsValid)\n\t\t\t\tthrow new ArgumentException (\"The uid is invalid.\", nameof (uid));\n\n\t\t\tif (offset < 0)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (offset));\n\n\t\t\tif (count < 0)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (count));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tif (count == 0) {\n\t\t\t\tctx = null;\n\t\t\t\tic = null;\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tic = new ImapCommand (Engine, cancellationToken, this, \"UID FETCH %u (BODY.PEEK[]<%d.%d>)\\r\\n\", uid.Id, offset, count);\n\t\t\tic.RegisterUntaggedHandler (\"FETCH\", FetchStreamHandler);\n\t\t\tic.UserData = ctx = new FetchStreamContext (progress);\n\n\t\t\tEngine.QueueCommand (ic);\n\n\t\t\treturn true;\n\t\t}\n","sourceCodeStart":4172,"sourceCodeEnd":4208,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L4172-L4208","documentation":"ArgumentOutOfRangeException raised inside TryQueueGetStreamCommand in ImapFolderFetch: the 'count' parameter (number of bytes to fetch from the stream at 'offset') was negative or out of range relative to the buffer, so the stream-fetch command could not be queued. This is a generic argument-range validation, not a server error.","triggerScenarios":"Calling GetStream/GetStreamAsync (UID overload) with a negative count (ImapFolderFetch.cs:4190), typically from underflowed chunk math or mis-parsed sizes.","commonSituations":"Parsing byte counts from config strings into signed ints that go negative; subtracting larger progress counters; integer overflow wrapping negative.","solutions":["Clamp count with Math.Max(0, count) before calling.","Validate configured chunk sizes at startup.","Note count == 0 is allowed and returns an empty stream; only negatives fail."],"exampleFix":"// before\nvar count = remaining - fetched;\nvar stream = folder.GetStream(uid, offset, count);\n// after\nvar count = Math.Max(0, remaining - fetched);\nvar stream = folder.GetStream(uid, offset, count);","handlingStrategy":"validation","validationCode":"count = Math.Max(0, count);\nvar stream = folder.GetStream(uid, offset, count);","typeGuard":null,"tryCatchPattern":"try { stream = folder.GetStream(uid, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* fix chunk-size math */ }","preventionTips":["Clamp counts with Math.Max(0, ...) before calls.","Validate configured chunk sizes are positive at startup.","Use Math.Min to cap counts at remaining bytes."],"tags":["argument-out-of-range","imap","mailkit"],"backgroundTag":"argument-out-of-range","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}