{"record":{"id":"420f72b41edbf454","repo":"dotnet/wpf","slug":"buffer-address-passed-to-gettext-cannot-be-null","errorCode":null,"errorMessage":"Buffer address passed to GetText cannot be NULL.","messagePattern":"Buffer address passed to GetText cannot be NULL\\.","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs","lineNumber":111,"sourceCode":"\n        /// <summary>\n        /// Gets text content corresponding to current chunk.\n        /// </summary>\n        /// <param name=\"bufCharacterCount\">size of buffer in characters</param>\n        /// <param name=\"pBuffer\">buffer pointer</param>\n        /// <remarks>Supported for indexing content of Package.</remarks>\n        void IFilter.GetText(ref uint bufCharacterCount, IntPtr pBuffer)\n        {\n            if (_filter == null)\n            {\n                throw new COMException(SR.FileToFilterNotLoaded,\n                    (int)FilterErrorCode.FILTER_E_ACCESS);\n            }\n\n            // NULL is not an acceptable value for pBuffer\n            if (pBuffer == IntPtr.Zero)\n            {\n                throw new NullReferenceException(SR.FilterNullGetTextBufferPointer);\n            }\n\n            // If there is 0 byte to write, this is a no-op.\n            if (bufCharacterCount == 0)\n            {\n                return;\n            }\n\n            // Because we should always return the string with null terminator, a buffer size\n            // of one character can hold the null terminator only, we can always write the \n            // terminator to the buffer and return directly.\n            if (bufCharacterCount == 1)\n            {\n                Marshal.WriteInt16(pBuffer, 0);\n                return;\n            }\n\n            // Record the original buffer size. bufCharacterCount may be changed later.","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs#L93-L129","documentation":"IFilter.GetText rejects a null (IntPtr.Zero) buffer before attempting to write chunk text into unmanaged memory. The COM/IFilter contract requires the caller (the index service) to supply a valid pre-allocated character buffer sized by bufCharacterCount; a zero pointer means the caller violated that contract, so the filter throws NullReferenceException rather than corrupting memory via Marshal writes.","triggerScenarios":"Calling IFilter.GetText with pBuffer == IntPtr.Zero and a non-zero bufCharacterCount.","commonSituations":"Interop callers whose Marshal.AllocHGlobal allocation failed or returned IntPtr.Zero, or callers mistakenly passing IntPtr.Zero to probe required buffer size.","solutions":["Allocate a valid buffer (Marshal.AllocHGlobal) sized bufCharacterCount * sizeof(char) before calling GetText","Check buffer allocation success before calling GetText","If the count is zero GetText is a no-op, but never pass IntPtr.Zero with a nonzero count"],"exampleFix":"// before\nfilter.GetText(ref count, IntPtr.Zero);\n// after\nIntPtr buffer = Marshal.AllocHGlobal((int)(count * 2));\ntry { filter.GetText(ref count, buffer); }\nfinally { Marshal.FreeHGlobal(buffer); }","handlingStrategy":"validation","validationCode":"if (pBuffer == IntPtr.Zero && bufCharacterCount > 0)\n    throw new ArgumentException(\"GetText buffer must be allocated\", nameof(pBuffer));","typeGuard":null,"tryCatchPattern":"try { filter.GetText(ref count, buffer); }\ncatch (NullReferenceException) { /* reallocate buffer and retry */ }","preventionTips":["Always allocate unmanaged buffers before interop calls","Never use IntPtr.Zero to probe required buffer sizes","Free buffers in finally blocks to avoid cascading allocation failures"],"tags":["wpf","xps","ifilter","interop","null"],"backgroundTag":"null-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"}