{"record":{"id":"e89b05983755a9e6","repo":"git-ecosystem/git-credential-manager","slug":"argument-must-be-less-than-or-equal-to-upper","errorCode":null,"errorMessage":"Argument must be less than or equal to {upper}.","messagePattern":"Argument must be less than or equal to (.+?)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Core/EnsureArgument.cs","lineNumber":91,"sourceCode":"                throw new ArgumentOutOfRangeException(name, \"Argument must be negative.\");\n            }\n        }\n\n        public static void InRange(int arg, string name, int lower, int upper, bool lowerInclusive = true, bool upperInclusive = true)\n        {\n            if (lowerInclusive && arg < lower)\n            {\n                throw new ArgumentOutOfRangeException(name, $\"Argument must be greater than or equal to {lower}.\");\n            }\n\n            if (!lowerInclusive && arg <= lower)\n            {\n                throw new ArgumentOutOfRangeException(name, $\"Argument must be strictly greater than {lower}.\");\n            }\n\n            if (upperInclusive && arg > upper)\n            {\n                throw new ArgumentOutOfRangeException(name, $\"Argument must be less than or equal to {upper}.\");\n            }\n\n            if (!upperInclusive && arg >= upper)\n            {\n                throw new ArgumentOutOfRangeException(name, $\"Argument must be strictly less than {upper}.\");\n            }\n        }\n    }\n}\n","sourceCodeStart":73,"sourceCodeEnd":101,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/EnsureArgument.cs#L73-L101","documentation":"This ArgumentOutOfRangeException is thrown by EnsureArgument.InRange when the argument exceeds the upper bound while upperInclusive is true. The value must satisfy arg <= upper. The interpolated message includes the actual upper bound and the parameter name is attached.","triggerScenarios":"Calling EnsureArgument.InRange(arg, name, lower, upper) (default upperInclusive: true) with arg > upper, e.g., an index equal to a collection's Count (one past the last element), or a percentage above 100.","commonSituations":"Classic off-by-one: using count instead of count - 1 as the inclusive max index; computing a page number exceeding the page count; user input above the allowed maximum.","solutions":["Verify the upper bound is correct: for zero-based inclusive indexes use length - 1, not length","Clamp the value with Math.Min(value, upper) when exceeding the max is harmless","Fix the computation (pagination, percentage) that produces an out-of-range value","Validate user/config input at the boundary with a friendly message"],"exampleFix":"// before\nEnsureArgument.InRange(index, nameof(index), 0, list.Count); // index == list.Count\n// after\nEnsureArgument.InRange(index, nameof(index), 0, list.Count - 1);","handlingStrategy":"validation","validationCode":"if (value > upper) value = upper; // clamp\nEnsureArgument.InRange(value, nameof(value), lower, upper);","typeGuard":"static bool IsWithin(int v, int lower, int upper) => v >= lower && v <= upper;","tryCatchPattern":"try { EnsureArgument.InRange(index, nameof(index), 0, list.Count - 1); }\ncatch (ArgumentOutOfRangeException ex)\n{\n    logger.LogWarning(\"{Param}={Value} exceeds max {Max}; clamping\", ex.ParamName, index, list.Count - 1);\n    index = list.Count - 1;\n}","preventionTips":["Use count - 1 (not count) as the inclusive max for zero-based indexes","Clamp with Math.Min(value, upper) when overflow is benign","Cap user-supplied numbers (page, percentage) before validation","Test boundary values: upper, upper - 1"],"tags":["argument-validation","range-check","bounds"],"backgroundTag":"value-out-of-range","analyzedSha":"e8ce762cd04b4100ae637b5fbf39ef9d0a96561e","analyzedAt":"2026-09-11T17:15:08.753Z","contentChangedAt":"2026-09-11T17:15:08.753Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}