From 0ed36a91f76f70029f64818d1b1144686158c533 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Mon, 30 Oct 2023 18:28:27 -0700 Subject: [PATCH] Enforce `clippy::cast_possible_truncation`. For now, just put `#[allow(...)]` directives in the places where the conversions are done. We'll follow up in the future with the correct replacement for `as` for each case, as several PRs. --- src/aead/aes.rs | 2 ++ src/aead/chacha.rs | 1 + src/aead/gcm/gcm_nohw.rs | 1 + src/ec/suite_b/ecdsa/signing.rs | 2 ++ src/ec/suite_b/ops/p256.rs | 1 + src/ec/suite_b/ops/p384.rs | 1 + src/io/der_writer.rs | 1 + src/lib.rs | 1 + 8 files changed, 10 insertions(+) diff --git a/src/aead/aes.rs b/src/aead/aes.rs index bc6ec9c895..f87427b584 100644 --- a/src/aead/aes.rs +++ b/src/aead/aes.rs @@ -49,6 +49,7 @@ fn set_encrypt_key( key: &mut AES_KEY, ) -> Result<(), error::Unspecified> { // Unusually, in this case zero means success and non-zero means failure. + #[allow(clippy::cast_possible_truncation)] if 0 == unsafe { f(bytes.as_ptr(), key_bits.as_usize_bits() as c::uint, key) } { Ok(()) } else { @@ -111,6 +112,7 @@ fn ctr32_encrypt_blocks_( assert_eq!(in_out_len % BLOCK_LEN, 0); let blocks = in_out_len / BLOCK_LEN; + #[allow(clippy::cast_possible_truncation)] let blocks_u32 = blocks as u32; assert_eq!(blocks, polyfill::usize_from_u32(blocks_u32)); diff --git a/src/aead/chacha.rs b/src/aead/chacha.rs index 73d2f1685c..660cf34c2d 100644 --- a/src/aead/chacha.rs +++ b/src/aead/chacha.rs @@ -272,6 +272,7 @@ mod tests { // behavior of ChaCha20 implementation changes dependent on the // length of the input. for len in 0..=input.len() { + #[allow(clippy::cast_possible_truncation)] chacha20_test_case_inner( &key, &nonce, diff --git a/src/aead/gcm/gcm_nohw.rs b/src/aead/gcm/gcm_nohw.rs index 6898b6eaa0..d9b03a61ec 100644 --- a/src/aead/gcm/gcm_nohw.rs +++ b/src/aead/gcm/gcm_nohw.rs @@ -27,6 +27,7 @@ use crate::polyfill::ArraySplitMap; #[cfg(target_pointer_width = "64")] fn gcm_mul64_nohw(a: u64, b: u64) -> (u64, u64) { + #[allow(clippy::cast_possible_truncation)] #[inline(always)] fn lo(a: u128) -> u64 { a as u64 diff --git a/src/ec/suite_b/ecdsa/signing.rs b/src/ec/suite_b/ecdsa/signing.rs index f8e8adce58..587086c89b 100644 --- a/src/ec/suite_b/ecdsa/signing.rs +++ b/src/ec/suite_b/ecdsa/signing.rs @@ -14,6 +14,8 @@ //! ECDSA Signatures using the P-256 and P-384 curves. +#![allow(clippy::cast_possible_truncation)] // XXX + use super::digest_scalar::digest_scalar; use crate::{ arithmetic::montgomery::*, diff --git a/src/ec/suite_b/ops/p256.rs b/src/ec/suite_b/ops/p256.rs index d146916b54..92bd3c9612 100644 --- a/src/ec/suite_b/ops/p256.rs +++ b/src/ec/suite_b/ops/p256.rs @@ -248,6 +248,7 @@ fn p256_scalar_inv_to_mont(a: &Scalar) -> Scalar { // 1011110011100110111110101010110110100111000101111001111010000100 // 1111001110111001110010101100001011111100011000110010010101001111 + #[allow(clippy::cast_possible_truncation)] static REMAINING_WINDOWS: [(u8, u8); 26] = [ (6, B_101111 as u8), (2 + 3, B_111 as u8), diff --git a/src/ec/suite_b/ops/p384.rs b/src/ec/suite_b/ops/p384.rs index 90b3f18c37..369db2b289 100644 --- a/src/ec/suite_b/ops/p384.rs +++ b/src/ec/suite_b/ops/p384.rs @@ -220,6 +220,7 @@ fn p384_scalar_inv_to_mont(a: &Scalar) -> Scalar { // 0101100000011010000011011011001001001000101100001010011101111010 // 1110110011101100000110010110101011001100110001010010100101110001 + #[allow(clippy::cast_possible_truncation)] static REMAINING_WINDOWS: [(u8, u8); 39] = [ (2, B_11 as u8), (3 + 3, B_111 as u8), diff --git a/src/io/der_writer.rs b/src/io/der_writer.rs index 4c166eb9eb..db0d0cbf9e 100644 --- a/src/io/der_writer.rs +++ b/src/io/der_writer.rs @@ -39,6 +39,7 @@ pub(crate) fn write_all(tag: Tag, write_value: &dyn Fn(&mut dyn Accumulator)) -> output.into() } +#[allow(clippy::cast_possible_truncation)] fn write_tlv(output: &mut dyn Accumulator, tag: Tag, write_value: F) where F: Fn(&mut dyn Accumulator), diff --git a/src/lib.rs b/src/lib.rs index 60b0f91826..c7ff22efeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,6 +60,7 @@ #![warn( clippy::unnecessary_cast, clippy::cast_lossless, + clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_precision_loss, clippy::cast_sign_loss