{"record":{"id":"795f4bed91c74a21","repo":"dotnet/wpf","slug":"sr-textrangeprovider-invalidparametervalue","errorCode":null,"errorMessage":"SR.TextRangeProvider_InvalidParameterValue","messagePattern":"SR\\.TextRangeProvider_InvalidParameterValue","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs","lineNumber":1854,"sourceCode":"            Normalize();\n\n            AutomationPeer peer = GetEnclosingAutomationPeer(_start, _end);\n            Invariant.Assert(peer != null);\n            IRawElementProviderSimple provider = ProviderFromPeer(peer);\n            Invariant.Assert(provider != null);\n            return provider;\n        }\n\n        /// <summary>\n        /// Retrieves the text of the range.\n        /// </summary>\n        /// <param name=\"maxLength\">Specifies the maximum length of the string to return or -1 if no limit is requested.</param>\n        /// <returns>The text of the range possibly truncated to the specified limit.</returns>\n        string ITextRangeProvider.GetText(int maxLength)\n        {\n            if (maxLength < 0 && maxLength != -1)\n            {\n                throw new ArgumentException(SR.Format(SR.TextRangeProvider_InvalidParameterValue, maxLength, \"maxLength\"));\n            }\n\n            Normalize();\n\n            string text = TextRangeBase.GetTextInternal(_start, _end);\n            return (text.Length <= maxLength || maxLength == -1) ? text : text.Substring(0, maxLength);\n        }\n\n        /// <summary>\n        /// Moves the range the specified number of units in the text.  Note that the text is not altered.  Instead the\n        /// range spans a different part of the text.\n        /// If the range is degenerate, this method tries to move the insertion point count units.  If the range is nondegenerate \n        /// and count is greater than zero, this method collapses the range at its end point, moves the resulting range forward \n        /// to a unit boundary (if it is not already at one), and then tries to move count - 1 units forward. If the range is \n        /// nondegenerate and count is less than zero, this method collapses the range at the starting point, moves the resulting \n        /// range backward to a unit boundary (if it isn't already at one), and then tries to move |count| - 1 units backward. \n        /// Thus, in both cases, collapsing a nondegenerate range, whether or not moving to the start or end of the unit following \n        /// the collapse, counts as a unit.","sourceCodeStart":1836,"sourceCodeEnd":1872,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs#L1836-L1872","documentation":"TextRangeAdaptor's ITextRangeProvider.GetText(int maxLength) throws ArgumentException (SR.TextRangeProvider_InvalidParameterValue) when maxLength is negative and not exactly -1. Per the UI Automation contract, only -1 means 'no limit'; any other negative value is an invalid parameter.","triggerScenarios":"Calling ITextRangeProvider.GetText(maxLength) on a WPF text range with values like -2, -10, or any negative int other than -1.","commonSituations":"Automation clients computing a length limit via arithmetic that underflows (e.g. desiredLength - extra), passing unvalidated ints from config or P/Invoke marshalling, or mixing conventions where 0/negative means 'unlimited'.","solutions":["Pass -1 explicitly for unlimited text, or a non-negative value for truncation, before calling GetText.","Clamp/normalize negative values: if (maxLength < 0 && maxLength != -1) maxLength = -1.","Catch ArgumentException in wrapper layers and log the offending maxLength value to find the caller passing bad limits."],"exampleFix":"// before\nstring text = range.GetText(requested - overhead); // may be negative\n\n// after\nint maxLength = requested - overhead;\nif (maxLength < 0) maxLength = -1; // -1 means no limit\nstring text = range.GetText(maxLength);","handlingStrategy":"validation","validationCode":"if (maxLength < 0 && maxLength != -1)\n    throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, \"Must be >= 0 or exactly -1\");\nstring text = range.GetText(maxLength);","typeGuard":"static bool IsValidGetTextLimit(int n) => n >= 0 || n == -1;","tryCatchPattern":null,"preventionTips":["Always pass -1 (not 0 or a negative sentinel) for unlimited text in UI Automation GetText.","Clamp computed limits to >= 0 or -1 before calling range APIs.","Document the -1 convention in any automation wrapper you build."],"tags":["wpf","ui-automation","argument-validation","out-of-range"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}