{"record":{"id":"5063f8fa792b3276","repo":"git-ecosystem/git-credential-manager","slug":"argument-must-be-positive-or-zero-non-negative","errorCode":null,"errorMessage":"Argument must be positive or zero (non-negative).","messagePattern":"Argument must be positive or zero \\(non-negative\\)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Core/EnsureArgument.cs","lineNumber":49,"sourceCode":"                throw new ArgumentException(\"Argument cannot be empty or white space.\", name);\n            }\n        }\n\n        public static void AbsoluteUri(Uri arg, string name)\n        {\n            NotNull(arg, name);\n\n            if (!arg.IsAbsoluteUri)\n            {\n                throw new ArgumentException(\"Argument must be an absolute URI.\", name);\n            }\n        }\n\n        public static void PositiveOrZero(int arg, string name)\n        {\n            if (arg < 0)\n            {\n                throw new ArgumentOutOfRangeException(name, \"Argument must be positive or zero (non-negative).\");\n            }\n        }\n\n        public static void Positive(int arg, string name)\n        {\n            if (arg <= 0)\n            {\n                throw new ArgumentOutOfRangeException(name, \"Argument must be positive.\");\n            }\n        }\n\n        public static void NegativeOrZero(int arg, string name)\n        {\n            if (arg > 0)\n            {\n                throw new ArgumentOutOfRangeException(name, \"Argument must be negative or zero (non-positive).\");\n            }\n        }","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/EnsureArgument.cs#L31-L67","documentation":"This ArgumentOutOfRangeException is thrown by EnsureArgument.PositiveOrZero when an int argument is negative. The parameter is expected to be non-negative (>= 0), e.g., a count, index, timeout, or capacity. The exception carries the parameter name for diagnosis.","triggerScenarios":"Calling EnsureArgument.PositiveOrZero(arg, name) with arg < 0, e.g., a computed value that underflowed, a config value like \"-1\", or a subtracted count that went below zero.","commonSituations":"Passing a -1 sentinel value where only >= 0 is allowed; computing a remaining-count that went negative; env var or config parsed to a negative number; integer subtraction overflow to negative.","solutions":["Ensure the value is >= 0 before the call: clamp with Math.Max(0, value) if negatives are meaningless","Find why the value is negative (sentinel usage, subtraction, config) and fix the producer","If -1 is a legitimate \"unset\" sentinel, use a nullable int (int?) or a separate flag instead","Validate config/env values at startup with a clear error message"],"exampleFix":"// before\nEnsureArgument.PositiveOrZero(retries, nameof(retries)); // retries == -1\n// after\nvar safeRetries = retries == -1 ? 0 : Math.Max(0, retries);\nEnsureArgument.PositiveOrZero(safeRetries, nameof(safeRetries));","handlingStrategy":"validation","validationCode":"if (value < 0) throw new InvalidOperationException($\"{name} must be >= 0, was {value}\");\n// or clamp: value = Math.Max(0, value);","typeGuard":"static bool IsNonNegative(int v) => v >= 0;","tryCatchPattern":"try { EnsureArgument.PositiveOrZero(retries, nameof(retries)); }\ncatch (ArgumentOutOfRangeException ex)\n{\n    logger.LogError(\"{Param} was {Value}; must be non-negative\", ex.ParamName, retries);\n    throw new ConfigurationErrorsException(\"Retries must be >= 0\", ex);\n}","preventionTips":["Clamp computed values with Math.Max(0, x) where negatives are meaningless","Avoid -1 sentinels; use int? or explicit flags for 'unset'","Validate config values at startup with documented minimums","Watch for subtraction that can underflow below zero"],"tags":["argument-validation","range-check","csharp"],"backgroundTag":"argument-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"}