{"record":{"id":"ce479db02c75e70a","repo":"stride3d/stride","slug":"there-should-be-5-words","errorCode":null,"errorMessage":"There should be 5 words","messagePattern":"There should be 5 words","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Spirv/Parsing/RefHeader.cs","lineNumber":20,"sourceCode":"\n/// <summary>\n/// A spirv header parser\n/// </summary>\npublic readonly ref struct RefHeader\n{\n    internal Span<int> Words { get; init; }\n    public uint MagicNumber { get => unchecked((uint)Words[0]); set => Words[0] = unchecked((int)value); }\n    public SpirvVersion VersionNumber { get => Words[1]; set => Words[1] = value; }\n    public int GeneratorMagicNumber { get => Words[2]; set => Words[2] = value; }\n    public int Bound { get => Words[3]; set => Words[3] = value; }\n    public int Schema { get => Words[4]; set => Words[4] = value; }\n\n    public string Version => $\"{VersionNumber >> 16}.{(VersionNumber >> 8) & 0x00FF}\";\n\n    public RefHeader(Span<int> words)\n    {\n        if (words.Length != 5)\n            throw new ArgumentException(\"There should be 5 words\");\n        Words = words;\n    }\n\n    public bool IsValidMagic => MagicNumber == Specification.MagicNumber;\n\n}\n","sourceCodeStart":2,"sourceCodeEnd":27,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Spirv/Parsing/RefHeader.cs#L2-L27","documentation":"RefHeader is the SPIR-V module header wrapper over the 5 mandatory header words (magic number, version, generator, bound, schema). The constructor validates the input span length and throws ArgumentException if it is not exactly 5 words. A wrong length means the buffer passed is not a valid SPIR-V header slice.","triggerScenarios":"Calling new RefHeader(...) with a Span<int> whose Length != 5 — e.g. passing the whole module, an empty span, or a misaligned slice of the code words.","commonSituations":"Reading a SPIR-V file and slicing the wrong number of leading ints; interpreting the module as bytes instead of 32-bit words; an empty/truncated .spv file (0 or <20 bytes).","solutions":["Slice exactly the first 5 ints (first 20 bytes) of the module before constructing RefHeader","Check the file is at least 20 bytes and load it as int words (little-endian) not raw bytes","Validate the magic number 0x07230203 after construction via IsValidMagic","Skip a known container prefix if the file is a SPIR-V container rather than a bare module"],"exampleFix":"// before\nvar header = new RefHeader(words);\n// after\nif (words.Length != 5)\n    throw new ArgumentException($\"Expected 5 header words, got {words.Length}\");\nvar header = new RefHeader(words.Slice(0, 5).ToArray());","handlingStrategy":"validation","validationCode":"if (words.Length != 5)\n    throw new ArgumentException($\"SPIR-V header needs exactly 5 words, got {words.Length}\");","typeGuard":"bool IsSpirvHeader(ReadOnlySpan<int> w) => w.Length == 5 && w[0] == 0x07230203;","tryCatchPattern":"try { var header = new RefHeader(words); }\ncatch (ArgumentException ex)\n{\n    logger.LogError(ex, \"Not a SPIR-V header slice\");\n    return null;\n}","preventionTips":["Always slice words.AsSpan(0, 5) before constructing the header","Load .spv files as 32-bit words, not bytes","Check file length >= 20 bytes before parsing","Validate header.IsValidMagic afterwards"],"tags":["spirv","argument-validation","header"],"backgroundTag":"invalid-argument-format","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"}