{"record":{"id":"887dc646150044ee","repo":"BoundaryML/baml","slug":"original-size-does-not-fit-usize","errorCode":null,"errorMessage":"original size does not fit usize","messagePattern":"original size does not fit usize","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_events/src/value/record.rs","lineNumber":183,"sourceCode":"            crate::value::pb::ValueAvailability::Omitted => ValueAvailability::Omitted,\n            crate::value::pb::ValueAvailability::Lost => ValueAvailability::Lost,\n            crate::value::pb::ValueAvailability::Unspecified => {\n                return Err(io::Error::new(\n                    io::ErrorKind::InvalidData,\n                    \"value metadata omitted availability\",\n                ));\n            }\n        };\n        Ok(Self {\n            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}","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_events/src/value/record.rs#L165-L201","documentation":"ValueMetadataV1.original_size_bytes is an i64/u64 in proto, and when present it is converted to usize. If the stored value is negative or exceeds the platform usize (e.g. > u64::MAX on 32-bit, or a negative value), usize::try_from fails and the record conversion returns InvalidData with this message.","triggerScenarios":"Decoding a ValueMetadataV1 whose original_size_bytes is negative (corruption, bit-flip, signed/unsigned writer bug) or larger than usize::MAX on a 32-bit target.","commonSituations":"32-bit builds reading traces written on 64-bit machines with huge declared sizes, corrupted/truncated files, or writer bugs storing sentinel values like -1.","solutions":["Validate/repair the writer to never emit negative or absurd original_size_bytes values","Read the trace on a 64-bit platform if the values are legitimately large","Treat sizes > a sane bound as corruption and skip the record at ingest","Sanitize the proto before conversion (clamp or None-out implausible sizes)"],"exampleFix":"// before\nlet size = md.original_size_bytes.unwrap_or_default(); // i64 may be negative\n// after\nlet size = md.original_size_bytes.filter(|s| *s >= 0 && (*s as u64) <= usize::MAX as u64);","handlingStrategy":"type-guard","validationCode":"fn size_fits(v: Option<i64>) -> bool { v.map_or(true, |s| s >= 0 && (s as u64) <= usize::MAX as u64) }","typeGuard":"fn valid_original_size(md: &pb::ValueMetadataV1) -> bool { md.original_size_bytes.map_or(true, |s| s >= 0) }","tryCatchPattern":"ValueRecord::try_from(md).map_err(|e| if e.to_string().contains(\"original size\") { CorruptionKind::BadSize.into() } else { e })","preventionTips":["Never use negative sentinels for sizes in protos","Sanity-check sizes against a max bound before writing","Test readers on the smallest target platform (32-bit) if supported"],"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"}