{"record":{"id":"5be5d00571997ce2","repo":"clockworklabs/SpacetimeDB","slug":"length-didn-t-fit-in-u32","errorCode":null,"errorMessage":"length didn't fit in `u32`","messagePattern":"length didn't fit in `u32`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/data-structures/src/slim_slice.rs","lineNumber":1369,"sourceCode":"}\nimpl<'a> TryFrom<&'a str> for SlimStr<'a> {\n    type Error = LenTooLong<&'a str>;\n\n    #[inline]\n    fn try_from(s: &'a str) -> Result<Self, Self::Error> {\n        ensure_len_fits!(s);\n        // SAFETY: ^-- satisfies `len <= u32::MAX`.\n        Ok(unsafe { Self::from_str_unchecked(s) })\n    }\n}\n\n/// Converts `&str` into the slim limited version.\n///\n/// Panics when `str.len() > u32::MAX`.\n#[inline]\npub const fn from_str(s: &str) -> SlimStr<'_> {\n    if s.len() > u32::MAX as usize {\n        panic!(\"length didn't fit in `u32`\");\n    }\n\n    // SAFETY: ^-- satisfies `len <= u32::MAX`.\n    unsafe { SlimStr::from_str_unchecked(s) }\n}\n\n/// Converts `&str` into the owned slim limited version.\n///\n/// Panics when `str.len() > u32::MAX`.\n#[inline]\npub fn from_string(s: &str) -> SlimStrBox {\n    from_str(s).into()\n}\n\n// =============================================================================\n// Mutable string slice reference\n// =============================================================================\n","sourceCodeStart":1351,"sourceCodeEnd":1387,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/data-structures/src/slim_slice.rs#L1351-L1387","documentation":"SlimStr packs its length into a u32, so it cannot represent strings longer than u32::MAX bytes (about 4 GiB). `from_str` is a const fn that enforces this with a panic instead of returning an error (the doc comment on the function says exactly this). Hitting it means a string at or beyond the 4 GiB structural limit was passed into the slim-slice representation; `from_string` delegates to `from_str` and panics the same way, while the fallible path is `SlimStr::try_from(&str)`, which uses the ensure_len_fits! check and returns Err instead.","triggerScenarios":"Calling SlimStr::from_str / SlimStr::from_string / From<&str> for SlimStr with s.len() > u32::MAX as usize; the const fn panics before any allocation happens.","commonSituations":"Loading oversized base64 blobs or whole files into string fields; property/fuzz tests generating huge strings; a serialization bug passing an entire buffer where a small key was expected.","solutions":["Use the fallible conversion `SlimStr::try_from(s)` and handle the Err, instead of the panicking `from_str`.","Check `s.len() <= u32::MAX as usize` before converting and reject or chunk the input.","Move payloads that can grow unboundedly into bytes/blob columns instead of strings.","If the value legitimately exceeds 4 GiB, split it across rows; the limit is structural to SlimStr."],"exampleFix":"// before: panics when the string exceeds u32::MAX bytes\nlet slim = SlimStr::from_str(huge);\n\n// after: fallible TryFrom<&str> returns Err instead (uses ensure_len_fits!)\nlet slim = SlimStr::try_from(huge)\n    .map_err(|_| format!(\"string of {} bytes exceeds SlimStr u32 length limit\", huge.len()))?;","handlingStrategy":"validation","validationCode":"// prefer the fallible TryFrom<&str> (returns Err via ensure_len_fits!) over the panicking from_str\nlet slim = SlimStr::try_from(s)\n    .map_err(|_| format!(\"string of {} bytes exceeds SlimStr u32 length limit\", s.len()))?;\n\n// or pre-check explicitly before any SlimStr conversion\nif s.len() > u32::MAX as usize {\n    return Err(format!(\"string of {} bytes exceeds SlimStr u32 length limit\", s.len()));\n}","typeGuard":"fn fits_slim_str(s: &str) -> bool { s.len() <= u32::MAX as usize }","tryCatchPattern":null,"preventionTips":["Use SlimStr::try_from instead of SlimStr::from_str for untrusted input sizes.","Reject oversized inputs at the API boundary before datastore conversions.","Prefer bytes/blob columns for payloads that can grow unboundedly.","Assert size limits in fuzz/property tests that feed generated strings into slim slices."],"tags":["rust","capacity-limit","string","panic","spacetimedb"],"backgroundTag":"length-limit-exceeded","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}