{"record":{"id":"66c574b18c3c7317","repo":"jstedfast/MailKit","slug":"invalid-unique-identifier","errorCode":null,"errorMessage":"Invalid unique identifier.","messagePattern":"Invalid unique identifier\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/UniqueIdSet.cs","lineNumber":428,"sourceCode":"\t\t\t}\n\n\t\t\tranges.Add (new Range (uid, uid));\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Adds the unique identifier to the set.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the unique identifier to the set.\n\t\t/// </remarks>\n\t\t/// <param name=\"uid\">The unique identifier to add.</param>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"uid\"/> is invalid.\n\t\t/// </exception>\n\t\tpublic void Add (UniqueId uid)\n\t\t{\n\t\t\tif (!uid.IsValid)\n\t\t\t\tthrow new ArgumentException (\"Invalid unique identifier.\", nameof (uid));\n\n\t\t\tif (ranges.Count == 0) {\n\t\t\t\tranges.Add (new Range (uid.Id, uid.Id));\n\t\t\t\tcount++;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tswitch (SortOrder) {\n\t\t\tcase SortOrder.Descending:\n\t\t\t\tBinaryInsertDescending (uid.Id);\n\t\t\t\tbreak;\n\t\t\tcase SortOrder.Ascending:\n\t\t\t\tBinaryInsertAscending (uid.Id);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tAppend (uid.Id);\n\t\t\t\tbreak;\n\t\t\t}","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/UniqueIdSet.cs#L410-L446","documentation":"UniqueIdSet.Add validates that the UniqueId being inserted is valid (Uid > 0). Passing a default/uninitialized UniqueId (whose Id is 0 or invalid) throws ArgumentException, because a unique identifier of 0 cannot be represented in an IMAP UID set.","triggerScenarios":"Calling UniqueIdSet.Add with a UniqueId created via default(UniqueId), new UniqueId(0), a UniqueId from a failed fetch, or UniqueId.Invalid; also via AddRange/enumerables containing such values, and in unit tests exercising argument validation.","commonSituations":"Parsing header/list results where the server omitted UID (yielding UniqueId.Zero), passing default-initialized structs from deserialization, copying UIDs from a different folder where validity checks were skipped, or building a set before checking uid.IsValid.","solutions":["Check uid.IsValid (or uid.Id > 0) before calling Add and skip invalid entries.","Filter the source sequence with Where (uid => uid.IsValid) before AddRange.","Investigate why the invalid UniqueId was produced — typically a FETCH response without the UID item or fetch from a nonexistent folder.","If an invalid UID is legitimate for your flow, use a nullable representation (UniqueId?) instead of UniqueId.Invalid."],"exampleFix":"// before\nvar set = new UniqueIdSet ();\nforeach (var uid in fetchedUids)\n    set.Add (uid); // throws on UniqueId.Invalid\n// after\nvar set = new UniqueIdSet ();\nforeach (var uid in fetchedUids.Where (uid => uid.IsValid))\n    set.Add (uid);","handlingStrategy":"validation","validationCode":"if (uid.IsValid)\n    set.Add (uid);\n// or for batches:\nvar valid = fetchedUids.Where (u => u.IsValid).ToArray ();\nset.AddRange (valid);","typeGuard":"static bool IsValidUid (UniqueId uid) => uid.IsValid; // Id > 0","tryCatchPattern":"try {\n    set.Add (uid);\n} catch (ArgumentException ex) when (ex.ParamName == \"uid\") {\n    logger.LogWarning (\"Skipped invalid UniqueId (Id=0) from fetch response\");\n}","preventionTips":["Always check UniqueId.IsValid before adding to a UniqueIdSet","Filter FETCH results to items that actually returned a UID","Never pass default(UniqueId) or UniqueId.Invalid into set operations","Keep UIDs scoped to their source folder to avoid mixing invalid/meaningless values"],"tags":["imap","uniqueid","argument-validation","mailkit"],"backgroundTag":"invalid-argument-value","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}