{"record":{"id":"c527b51bb3a42b46","repo":"louthy/language-ext","slug":"the-minimum-value-for-nameof-bytescount-is-1","errorCode":null,"errorMessage":"The minimum value for {nameof(bytesCount)} is 1","messagePattern":"The minimum value for (.+?) is 1","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Prelude/Random/Prelude.Random.cs","lineNumber":49,"sourceCode":"    public static int random(int max)\n    {\n        var bytes = ArrayPool<byte>.Shared.Rent(4);\n        rnd.GetBytes(bytes);\n        bytes[wordTop] &= 0x7f;\n        var value = BitConverter.ToInt32(bytes, 0) % max;\n        ArrayPool<byte>.Shared.Return(bytes);\n        return value;\n    }\n\n    /// <summary>\n    /// Thread-safe cryptographically strong random base-64 string generator\n    /// </summary>\n    /// <param name=\"bytesCount\">number of bytes generated that are then \n    /// returned Base64 encoded</param>\n    /// <returns>Base64 encoded random string</returns>\n    public static string randomBase64(int bytesCount)\n    {\n        if (bytesCount < 1) throw new ArgumentException($\"The minimum value for {nameof(bytesCount)} is 1\");\n        var bytes = ArrayPool<byte>.Shared.Rent(bytesCount);\n        rnd.GetBytes(bytes);\n        var r = Convert.ToBase64String(bytes);\n        ArrayPool<byte>.Shared.Return(bytes);\n        return r;\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":57,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Prelude/Random/Prelude.Random.cs#L31-L57","documentation":"randomBase64 validates that bytesCount >= 1 and throws ArgumentException with 'The minimum value for bytesCount is 1' when a smaller value is passed. The method rents bytes from the shared ArrayPool and Base64-encodes the random bytes, so at least one byte is required.","triggerScenarios":"Calling Prelude.randomBase64(0) or randomBase64 with a negative int.","commonSituations":"Computing byte counts from parsed config/CLI values or division results that end up zero, e.g. randomBase64(length / 2) with length < 2.","solutions":["Ensure bytesCount >= 1 before calling (clamp or validate)","Fix the upstream computation producing 0 or negative byte counts","Catch ArgumentException if the caller can legitimately pass dynamic values"],"exampleFix":"// before\nvar token = randomBase64(userConfig.KeyBytes); // KeyBytes = 0\n// after\nvar token = randomBase64(Math.Max(1, userConfig.KeyBytes));","handlingStrategy":"validation","validationCode":"if (bytesCount < 1) throw new ArgumentOutOfRangeException(nameof(bytesCount)); // or clamp: var n = Math.Max(1, bytesCount);","typeGuard":"static bool ValidByteCount(int n) => n >= 1;","tryCatchPattern":"try { var s = randomBase64(n); }\ncatch (ArgumentException ex) { /* n was 0/negative — fix upstream value */ }","preventionTips":["Clamp derived byte counts: Math.Max(1, computed)","Validate config/CLI integers feeding randomBase64","Use a named minimum constant in callers"],"tags":["csharp","languageext","random","argument"],"backgroundTag":"argument-out-of-range","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}