leptos-rs/leptos · error

TEMPLATE FAILURE

Error message

TEMPLATE FAILURE

What it means

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.

Source

Thrown at const_str_slice_concat/src/lib.rs:14

#![no_std]
#![forbid(unsafe_code)]
#![deny(missing_docs)]

//! Utilities for const concatenation of string slices.

pub(crate) const MAX_TEMPLATE_SIZE: usize = 4096;

/// Converts a zero-terminated buffer of bytes into a UTF-8 string.
pub const fn str_from_buffer(buf: &[u8; MAX_TEMPLATE_SIZE]) -> &str {
    match core::ffi::CStr::from_bytes_until_nul(buf) {
        Ok(cstr) => match cstr.to_str() {
            Ok(str) => str,
            Err(_) => panic!("TEMPLATE FAILURE"),
        },
        Err(_) => panic!("TEMPLATE FAILURE"),
    }
}

/// Concatenates any number of static strings into a single array.
// credit to Rainer Stropek, "Constant fun," Rust Linz, June 2022
pub const fn const_concat(
    strs: &'static [&'static str],
) -> [u8; MAX_TEMPLATE_SIZE] {
    let mut buffer = [0; MAX_TEMPLATE_SIZE];
    let mut position = 0;
    let mut remaining = strs;

    while let [current, tail @ ..] = remaining {
        let x = current.as_bytes();
        let mut i = 0;

View on GitHub (pinned to 32d20f6c9d)

Solutions

  1. Reduce the total size of the concatenated strings, or raise the MAX_TEMPLATE_SIZE limit the crate exposes.
  2. Ensure every input to the template is valid UTF-8 static data.
  3. Check for missing/extra NUL terminators if feeding raw byte arrays.

Example fix

// before
const S: &str = const_str_slice_concat::const_concat(&["a".repeat(MAX_TEMPLATE_SIZE)]); // overflows buffer
// after
const S: &str = const_str_slice_concat::const_concat(&["shorter", " strings"]);
Defensive patterns

Strategy: validation

Validate before calling

const fn check_fits(parts: &[&str]) -> bool {
    let total: usize = parts.iter().map(|s| s.len()).sum::<usize>() + parts.len();
    total < const_str_slice_concat::MAX_TEMPLATE_SIZE
}
const _: () = assert!(check_fits(&["part1", "part2"]), "template exceeds MAX_TEMPLATE_SIZE");

Prevention

When it happens

Trigger: 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.

Common situations: Concatenating string literals whose combined length exceeds the MAX_TEMPLATE_SIZE buffer; accidentally using byte-string literals with invalid UTF-8 in a template.

Related errors


AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01). Data as JSON: /api/errors/2e55fddc438b330a. Report an issue: GitHub.