{"record":{"id":"a952b3edc0bc8332","repo":"stride3d/stride","slug":"objectid-value-doesn-t-match-expected-size","errorCode":null,"errorMessage":"ObjectId value doesn't match expected size.","messagePattern":"ObjectId value doesn't match expected size\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Storage/ObjectId.cs","lineNumber":57,"sourceCode":"    public uint Hash3 => hash3;\n    public uint Hash4 => hash4;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"ObjectId\"/> struct.\n    /// </summary>\n    /// <param name=\"hash\">The hash.</param>\n    /// <exception cref=\"System.ArgumentNullException\">hash</exception>\n    /// <exception cref=\"System.InvalidOperationException\">ObjectId value doesn't match expected size.</exception>\n    public ObjectId(byte[] hash)\n    {\n#if NET7_0_OR_GREATER\n        ArgumentNullException.ThrowIfNull(hash);\n#else\n        if (hash is null) throw new ArgumentNullException(nameof(hash));\n#endif // NET7_0_OR_GREATER\n\n        if (hash.Length != HashSize)\n            throw new InvalidOperationException(\"ObjectId value doesn't match expected size.\");\n\n        fixed (byte* hashSource = hash)\n        {\n            var hashSourceCurrent = (uint*)hashSource;\n            hash1 = *hashSourceCurrent++;\n            hash2 = *hashSourceCurrent++;\n            hash3 = *hashSourceCurrent++;\n            hash4 = *hashSourceCurrent;\n        }\n    }\n\n    public ObjectId(uint hash1, uint hash2, uint hash3, uint hash4)\n    {\n        this.hash1 = hash1;\n        this.hash2 = hash2;\n        this.hash3 = hash3;\n        this.hash4 = hash4;\n    }","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Storage/ObjectId.cs#L39-L75","documentation":"Thrown by the ObjectId(byte[] hash) constructor when the input byte array does not have the exact 16-byte length required to represent an ObjectId. The exception is declared in the XML docs for the constructor and fires when callers pass a hash shorter or longer than the fixed size, typically when deserializing or reconstructing an ObjectId from raw or user-supplied bytes.","triggerScenarios":"new ObjectId(someBytes) where someBytes.Length != 16 — e.g. passing a SHA-256 (32-byte) hash, a truncated hash, or a hex-decoded string of the wrong length.","commonSituations":"Storing/reading ObjectIds from custom storage where they were written with a different hash size; computing hashes with the wrong algorithm (e.g. SHA-256 instead of the 16-byte digest Stride expects).","solutions":["Hash with a 128-bit algorithm (e.g. MD5 or first 16 bytes as Stride intends) before constructing the ObjectId","Verify the byte array length equals ObjectId.HashSize before constructing","If reading stored data, ensure it was written with the full 16-byte ObjectId"],"exampleFix":"// before\nvar id = new ObjectId(sha256Hash); // 32 bytes\n// after\nvar md5 = MD5.HashData(data);\nvar id = new ObjectId(md5); // exactly 16 bytes","handlingStrategy":"validation","validationCode":"if (hash == null || hash.Length != 16) // ObjectId.HashSize\n    throw new ArgumentException(\"ObjectId requires exactly 16 bytes\", nameof(hash));\nvar id = new ObjectId(hash);","typeGuard":"bool IsValidObjectIdBytes(byte[]? b) => b is { Length: 16 };","tryCatchPattern":"try { id = new ObjectId(bytes); }\ncatch (InvalidOperationException e) when (e.Message.Contains(\"doesn't match expected size\"))\n{ /* re-hash with a 128-bit digest or repair stored bytes */ }","preventionTips":["Always produce ObjectIds from a 16-byte hash","Check ObjectId.HashSize before constructing from stored bytes","Standardize on ObjectId.Parse/from-string APIs instead of raw byte arrays where possible"],"tags":["hash","argument-length"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}