{"record":{"id":"2e55fddc438b330a","repo":"leptos-rs/leptos","slug":"template-failure","errorCode":null,"errorMessage":"TEMPLATE FAILURE","messagePattern":"TEMPLATE FAILURE","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"const_str_slice_concat/src/lib.rs","lineNumber":14,"sourceCode":"#![no_std]\n#![forbid(unsafe_code)]\n#![deny(missing_docs)]\n\n//! Utilities for const concatenation of string slices.\n\npub(crate) const MAX_TEMPLATE_SIZE: usize = 4096;\n\n/// Converts a zero-terminated buffer of bytes into a UTF-8 string.\npub const fn str_from_buffer(buf: &[u8; MAX_TEMPLATE_SIZE]) -> &str {\n    match core::ffi::CStr::from_bytes_until_nul(buf) {\n        Ok(cstr) => match cstr.to_str() {\n            Ok(str) => str,\n            Err(_) => panic!(\"TEMPLATE FAILURE\"),\n        },\n        Err(_) => panic!(\"TEMPLATE FAILURE\"),\n    }\n}\n\n/// Concatenates any number of static strings into a single array.\n// credit to Rainer Stropek, \"Constant fun,\" Rust Linz, June 2022\npub const fn const_concat(\n    strs: &'static [&'static str],\n) -> [u8; MAX_TEMPLATE_SIZE] {\n    let mut buffer = [0; MAX_TEMPLATE_SIZE];\n    let mut position = 0;\n    let mut remaining = strs;\n\n    while let [current, tail @ ..] = remaining {\n        let x = current.as_bytes();\n        let mut i = 0;\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/const_str_slice_concat/src/lib.rs#L1-L32","documentation":"str_from_buffer converts a NUL-terminated byte buffer built during const string concatenation into a &'static str at compile time. If the buffer is not a valid NUL-terminated sequence, or its contents are not valid UTF-8, const evaluation fails with 'TEMPLATE FAILURE'. This guards compile-time template construction in const_str_slice_concat.","triggerScenarios":"Using the const concat/template macros with a byte buffer that exceeds MAX_TEMPLATE_SIZE (truncating the NUL terminator) or containing non-UTF-8 bytes, evaluated at compile time.","commonSituations":"Concatenating string literals whose combined length exceeds the MAX_TEMPLATE_SIZE buffer; accidentally using byte-string literals with invalid UTF-8 in a template.","solutions":["Reduce the total size of the concatenated strings, or raise the MAX_TEMPLATE_SIZE limit the crate exposes.","Ensure every input to the template is valid UTF-8 static data.","Check for missing/extra NUL terminators if feeding raw byte arrays."],"exampleFix":"// before\nconst S: &str = const_str_slice_concat::const_concat(&[\"a\".repeat(MAX_TEMPLATE_SIZE)]); // overflows buffer\n// after\nconst S: &str = const_str_slice_concat::const_concat(&[\"shorter\", \" strings\"]);","handlingStrategy":"validation","validationCode":"const fn check_fits(parts: &[&str]) -> bool {\n    let total: usize = parts.iter().map(|s| s.len()).sum::<usize>() + parts.len();\n    total < const_str_slice_concat::MAX_TEMPLATE_SIZE\n}\nconst _: () = assert!(check_fits(&[\"part1\", \"part2\"]), \"template exceeds MAX_TEMPLATE_SIZE\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert combined length < MAX_TEMPLATE_SIZE with a const assertion.","Only pass valid UTF-8 &str literals into const templates.","Document buffer limits near template constants."],"tags":["const-eval","compile-time","utf-8","template"],"backgroundTag":"const-eval-template-failure","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}