{"record":{"id":"5d8015923d801891","repo":"pydantic/monty","slug":"u32-fits-usize-on-supported-targets","errorCode":null,"errorMessage":"u32 fits usize on supported targets","messagePattern":"u32 fits usize on supported targets","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/monty/src/modules/binascii.rs","lineNumber":427,"sourceCode":"            HeapData::LongInt(long) => {\n                // `&` on a `BigInt` is two's-complement, so this is Python's\n                // own `value & 0xffffffff` for negatives as well.\n                Ok((&long.0 & BigInt::from(u32::MAX)).to_u32().expect(\"masked below 2**32\"))\n            }\n            _ => Err(ExcType::type_error_not_integer(&value.py_type_name(vm))),\n        },\n        _ => Err(ExcType::type_error_not_integer(&value.py_type_name(vm))),\n    }\n}\n\n/// Encodes bytes as lowercase hex, inserting `sep` every `bytes_per_sep` bytes.\n///\n/// A positive `bytes_per_sep` groups from the right, so any short group leads;\n/// a negative one groups from the left. Zero, or no separator, means none.\nfn hex_encode(data: &[u8], sep: Option<u8>, bytes_per_sep: i32) -> Vec<u8> {\n    let group = match sep {\n        Some(_) if bytes_per_sep != 0 && !data.is_empty() => usize::try_from(bytes_per_sep.unsigned_abs())\n            .expect(\"u32 fits usize on supported targets\")\n            .min(data.len()),\n        // Without a separator, or with an empty input, emit one flat run.\n        _ => data.len().max(1),\n    };\n    // Groups are measured from the right when positive, so the leading group\n    // absorbs the remainder.\n    let lead = if bytes_per_sep > 0 {\n        match data.len() % group {\n            0 => group,\n            remainder => remainder,\n        }\n    } else {\n        group\n    };\n\n    let mut out = Vec::with_capacity(data.len() * 2 + data.len() / group);\n    for (index, byte) in data.iter().enumerate() {\n        if let Some(sep) = sep","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/pydantic/monty/blob/adc986b362e3961f407868cb118a99fe831b9e61/crates/monty/src/modules/binascii.rs#L409-L445","documentation":"Internal `expect` in `hex_encode` (binascii.hexlify): converts the `bytes_per_sep` magnitude from `u32` to `usize` after `unsigned_abs()`. On all supported platforms `usize` is at least 32 bits, so the conversion cannot fail. Not reachable through `binascii.hexlify` with any separator or `bytes_per_sep` value.","triggerScenarios":"Unreachable; would only matter on a hypothetical target where `usize` < 32 bits, or if `unsigned_abs()` were replaced with an unchecked cast.","commonSituations":"Contributor-only, e.g. platform-porting audits.","solutions":["No runtime action; the code already documents the platform assumption in the expect message."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use `usize::try_from(x.unsigned_abs())` for i32→usize conversions; never `as usize` on negative or large values."],"tags":["rust","internal-invariant","hexlify"],"backgroundTag":"internal-invariant-violation","analyzedSha":"adc986b362e3961f407868cb118a99fe831b9e61","analyzedAt":"2026-09-13T19:19:18.698Z","contentChangedAt":"2026-09-13T19:19:18.698Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}