{"record":{"id":"f8b236c5634bac71","repo":"BoundaryML/baml","slug":"retained-size-does-not-fit-usize","errorCode":null,"errorMessage":"retained size does not fit usize","messagePattern":"retained size does not fit usize","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_events/src/value/record.rs","lineNumber":193,"sourceCode":"            id: metadata.id,\n            codec,\n            availability,\n            original_size_bytes: metadata\n                .original_size_bytes\n                .map(usize::try_from)\n                .transpose()\n                .map_err(|_| {\n                    io::Error::new(\n                        io::ErrorKind::InvalidData,\n                        \"original size does not fit usize\",\n                    )\n                })?,\n            retained_size_bytes: metadata\n                .retained_size_bytes\n                .map(usize::try_from)\n                .transpose()\n                .map_err(|_| {\n                    io::Error::new(\n                        io::ErrorKind::InvalidData,\n                        \"retained size does not fit usize\",\n                    )\n                })?,\n            diagnostic: metadata.diagnostic,\n        })\n    }\n}\n\nimpl From<&ValueRef> for crate::value::pb::ValueMetadataV1 {\n    fn from(value_ref: &ValueRef) -> Self {\n        Self {\n            id: value_ref.id.clone(),\n            codec: match value_ref.codec {\n                ValueCodec::BamlOutboundValue => {\n                    crate::value::pb::ValueCodec::BamlOutboundValue as i32\n                }\n            },","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_events/src/value/record.rs#L175-L211","documentation":"Same class as the original-size check: ValueMetadataV1.retained_size_bytes is converted to usize during the TryFrom conversion, and a negative or usize-overflowing value makes the conversion fail with InvalidData and this message.","triggerScenarios":"Decoding a ValueMetadataV1 with retained_size_bytes negative (e.g. -1 sentinel from a buggy writer) or exceeding usize::MAX on narrow platforms.","commonSituations":"Corrupted trace files, 32-bit readers consuming 64-bit-written traces, writer bugs emitting sentinel or uninitialized size fields.","solutions":["Fix the producer to emit valid non-negative retained_size_bytes","Run the reader on a 64-bit platform when values are legitimately large","Skip/flag records with implausible retained sizes during ingest instead of failing the whole stream","Sanitize the proto (reject or None-out out-of-range sizes) before conversion"],"exampleFix":"// before\nlet size = md.retained_size_bytes.unwrap_or_default();\n// after\nlet size = u64::try_from(md.retained_size_bytes.unwrap_or_default()).ok().filter(|s| *s <= usize::MAX as u64);","handlingStrategy":"type-guard","validationCode":"fn retained_fits(v: Option<i64>) -> bool { v.map_or(true, |s| s >= 0 && (s as u64) <= usize::MAX as u64) }","typeGuard":"fn valid_retained_size(md: &pb::ValueMetadataV1) -> bool { md.retained_size_bytes.map_or(true, |s| s >= 0) }","tryCatchPattern":"ValueRecord::try_from(md).map_err(|e| if e.to_string().contains(\"retained size\") { CorruptionKind::BadSize.into() } else { e })","preventionTips":["Clamp retained sizes to realistic bounds at the writer","Reject negative sizes at serialization time","Run cross-platform (32/64-bit) reader tests on large traces"],"tags":["rust","protobuf","integer-overflow","deserialization"],"backgroundTag":"value-out-of-range","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}