{"record":{"id":"40366ea502dec18c","repo":"dotnet/wpf","slug":"e-invalidarg","errorCode":"E_INVALIDARG","errorMessage":"Count and contents of attributes passed to IFilter::Init do not match.","messagePattern":"Count and contents of attributes passed to IFilter::Init do not match\\.","errorType":"validation","errorClass":"COMException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs","lineNumber":61,"sourceCode":"        /// <returns>\n        /// IFILTER_FLAGS_NONE to indicate that the caller should not use the IPropertySetStorage\n        /// and IPropertyStorage interfaces to locate additional properties.\n        /// </returns>\n        IFILTER_FLAGS IFilter.Init(\n            [In] IFILTER_INIT grfFlags,\n            [In] uint cAttributes,\n            [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] FULLPROPSPEC[] aAttributes)\n        {\n            if (_filter == null)\n            {\n                throw new COMException(SR.FileToFilterNotLoaded,\n                    (int)NativeMethods.E_FAIL);\n            }\n\n            if (cAttributes > 0 && aAttributes == null)\n            {\n                // Attributes count and array do not match.\n                throw new COMException(SR.FilterInitInvalidAttributes,\n                    (int)NativeMethods.E_INVALIDARG);\n            }\n\n            return _filter.Init(grfFlags, cAttributes, aAttributes);\n        }\n\n        /// <summary>\n        /// Returns description of the next chunk.\n        /// </summary>\n        /// <returns>Chunk descriptor</returns>\n        STAT_CHUNK IFilter.GetChunk()\n        {\n            if (_filter == null)\n            {\n                throw new COMException(SR.FileToFilterNotLoaded,\n                    (int)FilterErrorCode.FILTER_E_ACCESS);\n            }\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs#L43-L79","documentation":"XpsFilter.Init validates its attribute arguments: if cAttributes > 0 but aAttributes is null, the count and the array do not match, so it throws COMException with E_INVALIDARG before delegating to the inner filter's Init.","triggerScenarios":"Calling IFilter.Init with cAttributes set to a positive value while passing null for the FULLPROPSPEC[] array.","commonSituations":"Callers building the attribute list conditionally but forgetting to zero cAttributes when the array ends up null; interop marshalling inconsistencies.","solutions":["Pass aAttributes as null together with cAttributes == 0","Populate the FULLPROPSPEC array so its length matches cAttributes","Guard the call: if the attribute array is null, pass 0 for cAttributes"],"exampleFix":"// before\nfilter.Init(flags, requestedAttributes.Count, null);\n// after\nfilter.Init(flags, requestedAttributes.Count, requestedAttributes.ToArray());","handlingStrategy":"validation","validationCode":"if (attrs == null) cAttributes = 0;\nelse if (cAttributes > 0 && attrs.Length == 0) cAttributes = 0;","typeGuard":null,"tryCatchPattern":"try { filter.Init(flags, cAttrs, attrs); }\ncatch (COMException ex) when (ex.ErrorCode == unchecked((int)0x80070057)) { /* retry with cAttrs=0, null */ }","preventionTips":["Derive cAttributes and the array length from the same collection","Pass null/0 when only full-text filtering is needed","Add contract checks around all IFilter interop calls"],"tags":["wpf","xps","ifilter","com","arguments"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}