{"record":{"id":"9921d14caa50cab3","repo":"clockworklabs/SpacetimeDB","slug":"must-be-16-bytes","errorCode":null,"errorMessage":"Must be 16 bytes","messagePattern":"Must be 16 bytes","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs","lineNumber":55,"sourceCode":"    /// 16 random bytes used for entropy.\n    /// </param>\n    /// <example>\n    /// <code>\n    /// var randomBytes = new byte[16];\n    /// RandomNumberGenerator.Fill(randomBytes);\n    /// var uuid = Uuid.FromRandomBytesV4(randomBytes);\n    /// Console.WriteLine(uuid);\n    /// // Output: 166a036f-90f3-714a-9731-c8814b70e326\n    /// </code>\n    /// </example>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown if <paramref name=\"randomBytes\"/> is not exactly 16 bytes long.\n    /// </exception>\n    public static Uuid FromRandomBytesV4(ReadOnlySpan<byte> randomBytes)\n    {\n        if (randomBytes.Length != 16)\n        {\n            throw new ArgumentException(\"Must be 16 bytes\", nameof(randomBytes));\n        }\n\n        Span<byte> bytes = stackalloc byte[16];\n        randomBytes.CopyTo(bytes);\n        bytes[6] = (byte)((bytes[6] & 0x0F) | 0x40); // version 4\n        bytes[8] = (byte)((bytes[8] & 0x3F) | 0x80); // variant RFC 4122\n\n        return new(U128.FromBytesBigEndian(bytes));\n    }\n\n    /// <summary>\n    /// Generate a <see cref=\"Uuid\"/> `v7` using a monotonic counter from 0 to 2^31-1,\n    /// a Unix timestamp in milliseconds, and 4 random bytes.\n    ///\n    /// <example>\n    /// <code>\n    /// int counter = 1;\n    /// var now = new Timestamp(1_686_000_000_000);","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs#L37-L73","documentation":"Uuid.FromRandomBytesV4 builds a version-4 UUID by overwriting the version and variant nibbles of exactly 16 caller-supplied random bytes. UUIDs are fixed-width 128-bit values, so any length other than 16 is rejected with ArgumentException.","triggerScenarios":"Passing a random span of length != 16, e.g. RandomNumberGenerator.GetBytes(8), reusing a 32-byte entropy buffer, or slicing a randomness pool incorrectly.","commonSituations":"Custom RNG plumbing shared across features; test fixtures with stub byte arrays of arbitrary size; converting from other systems' variable-length random identifiers.","solutions":["Request exactly 16 bytes: RandomNumberGenerator.GetBytes(new byte[16]) or stackalloc Span<byte> of 16","Slice the entropy source to 16 bytes before calling","Centralize UUID creation in one helper so buffer sizing is owned in one place"],"exampleFix":"// before\nvar bytes = RandomNumberGenerator.GetBytes(8);\nvar id = Uuid.FromRandomBytesV4(bytes); // ArgumentException: Must be 16 bytes\n\n// after\nvar bytes = RandomNumberGenerator.GetBytes(16);\nvar id = Uuid.FromRandomBytesV4(bytes);","handlingStrategy":"validation","validationCode":"static bool IsUuidV4RandomBytes(ReadOnlySpan<byte> b) => b.Length == 16;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Allocate the random buffer in the same statement as the call: Uuid.FromRandomBytesV4(RandomNumberGenerator.GetBytes(16))","Keep one helper per UUID version owning its buffer size","Size test fixtures to exactly 16 bytes"],"tags":["csharp","spacetimedb","uuid","random","bytemanipulation"],"backgroundTag":"byte-array-length-mismatch","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}