{"record":{"id":"489f77646ac4ac36","repo":"gfx-rs/wgpu","slug":"byte-slice-is-empty-not-spir-v","errorCode":null,"errorMessage":"byte slice is empty, not SPIR-V","messagePattern":"byte slice is empty, not SPIR-V","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"wgpu/src/util/spirv.rs","lineNumber":43,"sourceCode":"pub fn make_spirv(data: &[u8]) -> ShaderSource<'_> {\n    ShaderSource::SpirV(make_spirv_raw(data))\n}\n\n/// Check whether the byte slice has the SPIR-V magic number (in either byte order) and of an\n/// appropriate size, and panic with a suitable message when it is not.\n///\n/// Returns whether the endianness is opposite of native endianness (i.e. whether\n/// [`u32::swap_bytes()`] should be called.)\n///\n/// Note: this function’s checks are relied upon for the soundness of [`make_spirv_const()`].\n/// Undefined behavior will result if it does not panic when `bytes.len()` is not a multiple of 4.\n#[track_caller]\nconst fn assert_has_spirv_magic_number_and_length(bytes: &[u8]) -> bool {\n    // First, check the magic number.\n    // This way we give the best error for wrong formats.\n    // (Plus a special case for the empty slice.)\n    let found_magic_number: Option<bool> = match *bytes {\n        [] => panic!(\"byte slice is empty, not SPIR-V\"),\n        // This would be simpler as slice::starts_with(), but that isn't a const fn yet.\n        [b1, b2, b3, b4, ..] => {\n            let prefix = u32::from_ne_bytes([b1, b2, b3, b4]);\n            if prefix == SPIRV_MAGIC_NUMBER {\n                Some(false)\n            } else if prefix == const { SPIRV_MAGIC_NUMBER.swap_bytes() } {\n                // needs swapping\n                Some(true)\n            } else {\n                None\n            }\n        }\n        _ => None, // fallthrough case = between 1 and 3 bytes\n    };\n\n    match found_magic_number {\n        Some(needs_byte_swap) => {\n            // Note: this assertion is relied upon for the soundness of `make_spirv_const()`.","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/gfx-rs/wgpu/blob/3e11ff59bf3f9795d285ecc045014089640d7248/wgpu/src/util/spirv.rs#L25-L61","documentation":"Panic from the SPIR-V input sanity check: the byte slice is empty (or fails the length/magic check), so it cannot be valid SPIR-V. This guard is load-bearing for soundness — make_spirv_const dereferences words only after it passes; the faulty input is the empty data slice.","triggerScenarios":"Thrown at wgpu/src/util/spirv.rs:43 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check that the SPIR-V byte slice is non-empty and its length is a multiple of 4 before calling make_spirv","Verify the file was actually read (not zero-length) before constructing the shader source"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3e11ff59bf3f9795d285ecc045014089640d7248","analyzedAt":"2026-09-03T01:43:21.459Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T07:17:11.731Z"}