{"record":{"id":"1773e1a10021d006","repo":"jstedfast/MailKit","slug":"uri","errorCode":null,"errorMessage":"uri","messagePattern":"uri","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartCollection.cs","lineNumber":210,"sourceCode":"\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Finds the index of the body part matching the specified URI, if it exists.</para>\n\t\t/// <para>If the URI scheme is <c>\"cid\"</c>, then matching is performed based on the Content-Id header\n\t\t/// values, otherwise the Content-Location headers are used. If the provided URI is absolute and a child\n\t\t/// part's Content-Location is relative, then then the child part's Content-Location URI will be combined\n\t\t/// with the value of its Content-Base header, if available, otherwise it will be combined with the\n\t\t/// multipart/related part's Content-Base header in order to produce an absolute URI that can be\n\t\t/// compared with the provided absolute URI.</para>\n\t\t/// </remarks>\n\t\t/// <returns>The index of the part matching the specified URI if found; otherwise <c>-1</c>.</returns>\n\t\t/// <param name=\"uri\">The URI of the body part.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"uri\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\tpublic int IndexOf (Uri uri)\n\t\t{\n\t\t\tif (uri == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (uri));\n\n\t\t\tbool cid = uri.IsAbsoluteUri && uri.Scheme.Equals (\"cid\", StringComparison.OrdinalIgnoreCase);\n\n\t\t\tfor (int index = 0; index < Count; index++) {\n\t\t\t\tif (this[index] is not BodyPartBasic bodyPart)\n\t\t\t\t\tcontinue;\n\n\t\t\t\tif (uri.IsAbsoluteUri) {\n\t\t\t\t\tif (cid) {\n\t\t\t\t\t\tif (!string.IsNullOrEmpty (bodyPart.ContentId)) {\n\t\t\t\t\t\t\t// Note: we might have a Content-Id in the form \"<id@domain.com>\", so attempt to decode it\n\t\t\t\t\t\t\tvar id = MimeUtils.EnumerateReferences (bodyPart.ContentId!).FirstOrDefault () ?? bodyPart.ContentId;\n\n\t\t\t\t\t\t\tif (id == uri.AbsolutePath)\n\t\t\t\t\t\t\t\treturn index;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (bodyPart.ContentLocation != null) {\n\t\t\t\t\t\tif (!bodyPart.ContentLocation.IsAbsoluteUri)","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartCollection.cs#L192-L228","documentation":"BodyPartCollection.IndexOf(Uri) throws ArgumentNullException when the uri parameter is null. The library requires a non-null Uri to search for the body part whose Content-Id or Location matches, so it fails fast instead of silently returning a bogus index. This follows the standard .NET convention for mandatory arguments.","triggerScenarios":"Calling BodyPartCollection.IndexOf(Uri) with a null Uri reference, e.g. passing an unset field or the result of a lookup that failed to produce a Uri (indexOf(null)).","commonSituations":"Developers resolve a cid: URI from message text or a config value, and the parsing step returns null (unparseable cid, missing Content-Id); they then pass the null Uri straight into IndexOf without checking.","solutions":["Ensure the Uri passed to IndexOf is non-null; check for null before calling.","If the value may be a string, parse it with Uri.TryCreate and only call IndexOf when parsing succeeded.","If null legitimately means 'no reference', skip the IndexOf call and handle the absence explicitly instead."],"exampleFix":"// before\nvar index = bodyParts.IndexOf(cidUri);\n// after\nvar index = cidUri is null ? -1 : bodyParts.IndexOf(cidUri);","handlingStrategy":"validation","validationCode":"if (uri is null)\n    return -1; // or handle absence explicitly\nvar index = bodyParts.IndexOf(uri);","typeGuard":"bool HasCidUri(Uri? uri) => uri is not null && uri.IsAbsoluteUri && uri.Scheme.Equals(\"cid\", StringComparison.OrdinalIgnoreCase);","tryCatchPattern":"try {\n    index = bodyParts.IndexOf(uri);\n} catch (ArgumentNullException ex) {\n    // ex.ParamName == \"uri\"; treat as 'no reference to search for'\n    index = -1;\n}","preventionTips":["Parse cid references with Uri.TryCreate and skip failures instead of passing null.","Use nullable reference types (Uri?) and check for null before calling IndexOf.","Never assign lookup results that may be null directly to variables consumed by MailKit APIs."],"tags":["null-reference","argument-validation","mailkit"],"backgroundTag":"null-argument","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"}