{"record":{"id":"4fc9b000d4075d73","repo":"jstedfast/MailKit","slug":"offset-imapfolderfetch","errorCode":null,"errorMessage":"offset","messagePattern":"offset","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":4187,"sourceCode":"\t\t/// </exception>\n\t\tpublic override Task<MimeEntity> GetBodyPartAsync (int index, BodyPart part, CancellationToken cancellationToken = default, ITransferProgress? progress = null)\n\t\t{\n\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","sourceCodeStart":4169,"sourceCodeEnd":4205,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L4169-L4205","documentation":"TryQueueGetStreamCommand (UID variant) throws ArgumentOutOfRangeException(\"offset\") when offset < 0. The offset into the message stream must be non-negative to build a valid IMAP FETCH BODY[]<offset.size> section.","triggerScenarios":"Calling GetStream/GetStreamAsync (UID overload) with a negative offset (ImapFolderFetch.cs:4187), e.g. from integer underflow when computing a partial-range start.","commonSituations":"Computing offset as `total - chunkSize` when total < chunkSize; subtracting header size from a smaller value; uninitialized counter variables.","solutions":["Clamp the computed offset with Math.Max(0, offset).","Validate range arithmetic before calling the API.","Use Math.Clamp or explicit checks when chunking message streams."],"exampleFix":"// before\nvar offset = summary.Length - chunkSize;\nvar stream = folder.GetStream(uid, offset, chunkSize);\n// after\nvar offset = Math.Max(0, summary.Length - chunkSize);\nvar stream = folder.GetStream(uid, offset, chunkSize);","handlingStrategy":"validation","validationCode":"offset = Math.Max(0, offset);\nif (offset < 0) throw new InvalidOperationException(\"Offset computation failed.\");","typeGuard":null,"tryCatchPattern":"try { stream = folder.GetStream(uid, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"offset\") { /* fix range math */ }","preventionTips":["Clamp all computed offsets with Math.Max(0, ...).","Use checked arithmetic to catch underflow early.","Unit-test chunk-boundary math for sizes smaller than the chunk."],"tags":["argument-out-of-range","imap","offset","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"}