{"record":{"id":"cce380334edfb40d","repo":"pydantic/monty","slug":"masked-below-2-16","errorCode":null,"errorMessage":"masked below 2**16","messagePattern":"masked below 2\\*\\*16","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/monty/src/modules/binascii.rs","lineNumber":227,"sourceCode":"    // 2**32 rather than rejecting one out of range.\n    let seed = match crc {\n        None => 0,\n        Some(value) => wrapping_u32(value, vm)?,\n    };\n    let checksum = crc32(encode_input(data, vm)?.as_ref(), seed);\n    Ok(Value::Int(i64::from(checksum)))\n}\n\n/// `binascii.crc_hqx(data, crc)` — the CRC-16 the classic BinHex format used,\n/// resumable through `crc` exactly as `crc32` is.\nfn call_crc_hqx(vm: &mut VM<'_>, args: ArgValues) -> RunResult<Value> {\n    let CrcHqxArgs { data, crc } = CrcHqxArgs::from_args(args, vm)?;\n    defer_drop!(data, vm);\n    defer_drop!(crc, vm);\n\n    // The `I` format takes the seed modulo 2**32, then the body narrows it to\n    // the 16 bits the register holds — so `-1` and `0xffff` seed alike.\n    let seed = u16::try_from(wrapping_u32(crc, vm)? & 0xffff).expect(\"masked below 2**16\");\n    let checksum = crc_hqx(encode_input(data, vm)?.as_ref(), seed);\n    Ok(Value::Int(i64::from(checksum)))\n}\n\n/// `binascii.b2a_uu(data, *, backtick=False)` — one uuencoded line, newline\n/// included, for at most the 45 bytes the line-length byte can describe.\nfn call_b2a_uu(vm: &mut VM<'_>, args: ArgValues) -> RunResult<Value> {\n    let B2aUuArgs { data, backtick } = B2aUuArgs::from_args(args, vm)?;\n    defer_drop!(data, vm);\n    defer_drop!(backtick, vm);\n\n    let backtick = backtick.py_bool(vm)?;\n    let encoded = uu_encode(encode_input(data, vm)?.as_ref(), backtick)?;\n    Ok(allocate_bytes(encoded, vm.heap))\n}\n\n/// `binascii.a2b_uu(data)` — the inverse, tolerant of either padding\n/// convention and of a line truncated before its length byte promised.","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/pydantic/monty/blob/adc986b362e3961f407868cb118a99fe831b9e61/crates/monty/src/modules/binascii.rs#L209-L245","documentation":"Internal `expect` in `binascii.crc_hqx` argument handling: the 32-bit seed is masked with `0xffff` before `u16::try_from`, so the conversion cannot fail — the mask guarantees the value is below 2**16. This documents the narrowing invariant; Python callers cannot cause it to panic regardless of the `crc` seed they pass.","triggerScenarios":"Unreachable via `binascii.crc_hqx(data, crc)`; any int, bool, or arbitrary-precision int seed is reduced by `wrapping_u32` and masked first.","commonSituations":"Only a concern for contributors changing the seed handling order (e.g. removing the `& 0xffff` mask).","solutions":["No runtime action; if it fires, verify the `& 0xffff` mask was not dropped during a refactor."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always mask seeds to the register width before narrowing conversions in checksum code."],"tags":["rust","internal-invariant","crc"],"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-23T08:17:48.524Z"}