{"record":{"id":"a0f357c2f3f9756f","repo":"clockworklabs/SpacetimeDB","slug":"randombytes-must-be-exactly-4-bytes","errorCode":null,"errorMessage":"randomBytes must be exactly 4 bytes","messagePattern":"randomBytes must be exactly 4 bytes","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs","lineNumber":127,"sourceCode":"    /// </param>\n    /// <exception cref=\"InvalidOperationException\">\n    /// Thrown if the counter value is negative.\n    /// </exception>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown if <paramref name=\"randomBytes\"/> is not exactly 4 bytes long, or <paramref name=\"now\"/> is  before unix epoch.\n    /// </exception>\n    /// <returns>\n    /// A <see cref=\"Uuid\"/> `v7`.\n    /// </returns>\n    public static Uuid FromCounterV7(\n        ref int counter,\n        Timestamp now,\n        ReadOnlySpan<byte> randomBytes // must be length 4\n    )\n    {\n        if (randomBytes.Length != 4)\n        {\n            throw new ArgumentException(\"randomBytes must be exactly 4 bytes\", nameof(randomBytes));\n        }\n\n        if (counter < 0)\n        {\n            throw new InvalidOperationException(\"uuid counter must be non-negative\");\n        }\n\n        if (now.MicrosecondsSinceUnixEpoch < 0)\n        {\n            throw new ArgumentException(\"timestamp before unix epoch\", nameof(now));\n        }\n        var unixTsMs = now.MicrosecondsSinceUnixEpoch / 1_000;\n\n        // monotonic 31-bit\n        var counterVal = counter;\n        counter = (counter + 1) & 0x7FFF_FFFF;\n\n        Span<byte> bytes = stackalloc byte[16];","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs#L109-L145","documentation":"Uuid.FromCounterV7 assembles a time-ordered v7 UUID from a 48-bit millisecond timestamp, a 31-bit monotonic counter, and exactly 4 caller-supplied random bytes that fill the remaining random bits. A randomness span of any other length is rejected with ArgumentException before anything is generated.","triggerScenarios":"Passing a random span with length != 4 — commonly a 16-byte buffer reused from v4 code paths, or an empty span when randomness was not plumbed in.","commonSituations":"Refactoring shared RNG buffers across UUID versions (v4 wants 16 bytes, v7 wants 4); copying sample code without adjusting the size; test stubs sized for a different version.","solutions":["Provide exactly 4 random bytes: RandomNumberGenerator.GetBytes(4)","If your helper returns a fixed buffer, slice it: rng[..4]","Keep separate creation helpers per UUID version so each size expectation is explicit"],"exampleFix":"// before\nvar rand = RandomNumberGenerator.GetBytes(16);\nvar id = Uuid.FromCounterV7(ref counter, now, rand); // ArgumentException: randomBytes must be exactly 4 bytes\n\n// after\nvar rand = RandomNumberGenerator.GetBytes(4);\nvar id = Uuid.FromCounterV7(ref counter, now, rand);","handlingStrategy":"validation","validationCode":"static bool IsUuidV7RandomBytes(ReadOnlySpan<byte> b) => b.Length == 4;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Allocate exactly 4 bytes for v7 randomness (v4 needs 16 — do not share buffers)","Wrap FromCounterV7 in a single factory method that owns RNG sizing","Add unit tests asserting the helper's buffer width"],"tags":["csharp","spacetimedb","uuid","uuidv7","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"}