{"record":{"id":"ca35428bb62494d1","repo":"jstedfast/MailKit","slug":"count","errorCode":null,"errorMessage":"count","messagePattern":"count","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/MailFolder.cs","lineNumber":6215,"sourceCode":"\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"CommandException\">\n\t\t/// The command failed.\n\t\t/// </exception>\n\t\tpublic virtual Stream GetStream (UniqueId uid, BodyPart part, int offset, int count, CancellationToken cancellationToken = default, ITransferProgress? progress = null)\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 (part == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (part));\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\treturn GetStream (uid, part.PartSpecifier, offset, count, cancellationToken, progress);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously get a substream of the specified body part.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Asynchronously gets a substream of the body part. If the starting offset is beyond\n\t\t/// the end of the body part, an empty stream is returned. If the number of\n\t\t/// bytes desired extends beyond the end of the body part, a truncated stream\n\t\t/// will be returned.\n\t\t/// </remarks>\n\t\t/// <returns>The stream.</returns>\n\t\t/// <param name=\"uid\">The UID of the message.</param>\n\t\t/// <param name=\"part\">The desired body part.</param>\n\t\t/// <param name=\"offset\">The starting offset of the first desired byte.</param>\n\t\t/// <param name=\"count\">The number of bytes desired.</param>","sourceCodeStart":6197,"sourceCodeEnd":6233,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailFolder.cs#L6197-L6233","documentation":"MailFolder.GetStream(UniqueId uid, BodyPart part, int offset, int count, ...) throws ArgumentOutOfRangeException with the message \"count\" when a negative number of bytes to read is supplied. The count must be zero or positive because it defines the length of the partial IMAP fetch; a negative value would produce an invalid IMAP body-section range.","triggerScenarios":"Calling MailFolder.GetStream(uid, part, offset, count) with count < 0, commonly from computing count as remaining = total - offset when offset > total, or passing an unvalidated chunkSize variable.","commonSituations":"Chunked download logic where the last chunk calculation underflows; passing -1 as a sentinel for 'read everything' (not supported; use the overload without offset/count instead).","solutions":["Validate count >= 0 before calling; clamp to the remaining stream length","Use Math.Max(0, (int)(totalBytes - offset)) for remaining-bytes calculations","If you want the whole part, call the GetStream overload without offset/count rather than a negative count"],"exampleFix":"// before\nstream = folder.GetStream(uid, part, offset, total - offset); // underflows\n// after\nvar count = Math.Max(0, (int)(total - offset));\nstream = folder.GetStream(uid, part, offset, count);","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentException(\"count must be >= 0\", nameof(count));","typeGuard":"static bool IsValidCount(int count) => count >= 0;","tryCatchPattern":"try {\n    var stream = folder.GetStream(uid, part, offset, count);\n} catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") {\n    // recompute count from part size\n}","preventionTips":["Compute remaining-bytes counts with Math.Max(0, total - offset)","Use the no-offset/no-count overload for full-part reads","Validate chunkSize from config is positive at startup"],"tags":["argument-validation","mailkit","imap","range"],"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-15T23:17:13.987Z"}