{"record":{"id":"e9910a83f99632db","repo":"dotnet/wpf","slug":"sr-idpnegativepagenumber","errorCode":null,"errorMessage":"SR.IDPNegativePageNumber","messagePattern":"SR\\.IDPNegativePageNumber","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/FlowDocumentPaginator.cs","lineNumber":73,"sourceCode":"        //-------------------------------------------------------------------\n\n        #region Public Methods\n\n\n        /// <summary>\n        /// Async version of <see cref=\"DocumentPaginator.GetPage\"/>\n        /// </summary>\n        /// <param name=\"pageNumber\">Page number.</param>\n        /// <param name=\"userState\">Unique identifier for the asynchronous task.</param>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// Throws ArgumentOutOfRangeException if PageNumber is negative.\n        /// </exception>\n        public override void GetPageAsync(int pageNumber, object userState)\n        {\n            // Page number cannot be negative.\n            if (pageNumber < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(pageNumber), SR.IDPNegativePageNumber);\n            }\n\n            // Reentrancy check.\n            if (_document.StructuralCache.IsFormattingInProgress)\n            {\n                throw new InvalidOperationException(SR.FlowDocumentFormattingReentrancy);\n            }\n            if (_document.StructuralCache.IsContentChangeInProgress)\n            {\n                throw new InvalidOperationException(SR.TextContainerChangingReentrancyInvalid);\n            }\n\n            DocumentPage page = null;\n\n            if (!_backgroundPagination)\n            {\n                page = GetPage(pageNumber);\n            }","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/FlowDocumentPaginator.cs#L55-L91","documentation":"FlowDocumentPaginator.GetPageAsync throws ArgumentOutOfRangeException(SR.IDPNegativePageNumber) when the pageNumber argument is less than zero. Page numbers in the WPF pagination API are zero-based non-negative integers; a negative index has no meaning so the method validates and throws immediately.","triggerScenarios":"Calling GetPageAsync(-1, userState) or any negative page number — commonly from code computing page indices with uninitialized variables, off-by-one loops (for i = -1), or result of a failed lookup returning -1 passed straight through.","commonSituations":"Loop bounds computed as page-1 with page=0; caching code storing -1 for 'unknown page' and passing it to GetPageAsync; content position lookups (GetPageNumber) returning sentinel values that are fed to GetPageAsync.","solutions":["Validate pageNumber >= 0 before calling GetPageAsync and clamp or return early.","Check the source of the page number; replace -1 'unknown' sentinels with nullable state.","Ensure pagination loops start at 0 and stay below PageCount.","If a content-position lookup failed, do not pass the sentinel to GetPageAsync; handle the failure instead."],"exampleFix":"// before\npaginator.GetPageAsync(pageIndex - 1, null); // pageIndex==0 → -1\n// after\nif (pageIndex - 1 >= 0) {\n    paginator.GetPageAsync(pageIndex - 1, null);\n}","handlingStrategy":"validation","validationCode":"if (pageNumber < 0)\n    throw new ArgumentOutOfRangeException(nameof(pageNumber));\npaginator.GetPageAsync(pageNumber, userState);","typeGuard":"bool IsValidPageNumber(int n) => n >= 0;","tryCatchPattern":"try { paginator.GetPageAsync(n, state); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"pageNumber\") {\n    // log and clamp: n = Math.Max(0, n);\n}","preventionTips":["Validate page indices before use","Replace -1 'unknown' sentinels with nullable values","Check loop bounds: start at 0, stay below PageCount","Verify 1-based vs 0-based page number conversions"],"tags":["wpf","argumentoutofrange","pagination","argument-validation"],"backgroundTag":"argument-out-of-range","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"}