diff --git a/.github/workflows/deny.yml b/.github/workflows/deny.yml index ba293e6..4b3ddc7 100644 --- a/.github/workflows/deny.yml +++ b/.github/workflows/deny.yml @@ -13,4 +13,4 @@ jobs: uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - name: Run cargo-deny - uses: EmbarkStudios/cargo-deny-action@2d8c9929d8f9f42bedba73f1287cb0b479f07acb # v2.0.3 + uses: EmbarkStudios/cargo-deny-action@e2f4ede4a4e60ea15ff31bc0647485d80c66cfba # v2.0.4 diff --git a/Cargo.lock b/Cargo.lock index 4c8e6fd..cbaead0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "adler2" @@ -85,9 +85,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "arbitrary" @@ -153,14 +153,14 @@ dependencies = [ "sha1", "sha2", "tempfile", - "thiserror 2.0.3", + "thiserror 2.0.6", "toml_edit", "topological-sort", "tracing", "tracing-subscriber", "x509-cert", - "zerocopy 0.8.11", - "zerocopy-derive 0.8.11", + "zerocopy 0.8.13", + "zerocopy-derive 0.8.13", "zip", ] @@ -253,9 +253,9 @@ dependencies = [ [[package]] name = "cap-primitives" -version = "3.4.1" +version = "3.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82fa6c3f9773feab88d844aa50035a33fb6e7e7426105d2f4bb7aadc42a5f89a" +checksum = "8fc15faeed2223d8b8e8cc1857f5861935a06d06713c4ac106b722ae9ce3c369" dependencies = [ "ambient-authority", "fs-set-times", @@ -264,15 +264,15 @@ dependencies = [ "ipnet", "maybe-owned", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", "winx", ] [[package]] name = "cap-std" -version = "3.4.1" +version = "3.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f71b70818556b4fe2a10c7c30baac3f5f45e973f49fc2673d7c75c39d0baf5b" +checksum = "c3dbd3e8e8d093d6ccb4b512264869e1281cdb032f7940bd50b2894f96f25609" dependencies = [ "cap-primitives", "io-extras", @@ -282,9 +282,9 @@ dependencies = [ [[package]] name = "cap-tempfile" -version = "3.4.1" +version = "3.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "200812b29d9de495a9ba97dee1737f7def8907de53a0962843b6a955df921e22" +checksum = "1ffa1c0edc4958d742bab2e903e52f93ccee482072680e08d6ce0784873e65b1" dependencies = [ "cap-std", "rand", @@ -303,9 +303,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.2" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" +checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" dependencies = [ "jobserver", "libc", @@ -336,9 +336,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.21" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -346,9 +346,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.21" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", @@ -379,9 +379,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "cms" @@ -604,9 +604,9 @@ checksum = "cfc25fd417983cc7f203394ebb89eba18e2df1b0ac1be2673091b5aca52b595f" [[package]] name = "fastrand" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "fixedbitset" @@ -638,13 +638,13 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "fs-set-times" -version = "0.20.1" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "033b337d725b97690d86893f9de22b67b80dcc4e9ad815f348254c38119db8fb" +checksum = "5e2e6123af26f0f2c51cc66869137080199406754903cc926a7690401ce09cb4" dependencies = [ "io-lifetimes", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -769,19 +769,19 @@ dependencies = [ [[package]] name = "io-extras" -version = "0.18.3" +version = "0.18.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d45fd7584f9b67ac37bc041212d06bfac0700b36456b05890d36a3b626260eb" +checksum = "2285ddfe3054097ef4b2fe909ef8c3bcd1ea52a8f0d274416caebeef39f04a65" dependencies = [ "io-lifetimes", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] name = "io-lifetimes" -version = "2.0.3" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a611371471e98973dbcab4e0ec66c31a10bc356eeb4d54a0e05eac8158fe38c" +checksum = "06432fb54d3be7964ecd3649233cddf80db2832f47fec34c01f65b3d9d774983" [[package]] name = "ipnet" @@ -883,9 +883,9 @@ dependencies = [ [[package]] name = "logos-codegen" -version = "0.14.2" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b32eb6b5f26efacd015b000bfc562186472cd9b34bdba3f6b264e2a052676d10" +checksum = "5f3303189202bb8a052bcd93d66b6c03e6fe70d9c7c47c0ea5e974955e54c876" dependencies = [ "beef", "fnv", @@ -893,14 +893,15 @@ dependencies = [ "proc-macro2", "quote", "regex-syntax", + "rustc_version", "syn 2.0.90", ] [[package]] name = "logos-derive" -version = "0.14.2" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e5d0c5463c911ef55624739fc353238b4e310f0144be1f875dc42fec6bfd5ec" +checksum = "774a1c225576486e4fdf40b74646f672c542ca3608160d348749693ae9d456e6" dependencies = [ "logos-codegen", ] @@ -1057,9 +1058,9 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "passterm" -version = "2.0.4" +version = "2.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1e7a4c22a6cce262e8c126b3052e6baf05d4dd5699ef23ed6dadd85c2a1194a" +checksum = "150ca2316c7813c688677784f20bb0a9efab639415ae1961869863ee99a81e51" dependencies = [ "libc", ] @@ -1215,9 +1216,9 @@ dependencies = [ [[package]] name = "prost" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b0487d90e047de87f984913713b85c601c05609aad5b0df4b4573fbf69aa13f" +checksum = "2c0fef6c4230e4ccf618a35c59d7ede15dea37de8427500f50aff708806e42ec" dependencies = [ "bytes", "prost-derive", @@ -1225,11 +1226,10 @@ dependencies = [ [[package]] name = "prost-build" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c1318b19085f08681016926435853bbf7858f9c082d0999b80550ff5d9abe15" +checksum = "d0f3e5beed80eb580c68e2c600937ac2c4eedabdfd5ef1e5b7ea4f3fba84497b" dependencies = [ - "bytes", "heck", "itertools", "log", @@ -1246,9 +1246,9 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" +checksum = "157c5a9d7ea5c2ed2d9fb8f495b64759f7816c7eaea54ba3978f0d63000162e3" dependencies = [ "anyhow", "itertools", @@ -1259,9 +1259,9 @@ dependencies = [ [[package]] name = "prost-reflect" -version = "0.14.2" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b7535b02f0e5efe3e1dbfcb428be152226ed0c66cad9541f2274c8ba8d4cd40" +checksum = "20ae544fca2892fd4b7e9ff26cba1090cedf1d4d95c2aded1af15d2f93f270b8" dependencies = [ "logos", "miette", @@ -1272,9 +1272,9 @@ dependencies = [ [[package]] name = "prost-types" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4759aa0d3a6232fb8dbdb97b61de2c20047c68aca932c7ed76da9d788508d670" +checksum = "cc2f1e56baa61e93533aebc21af4d2134b70f66275e0fcdf3cbe43d77ff7e8fc" dependencies = [ "prost", ] @@ -1655,11 +1655,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.3" +version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +checksum = "8fec2a1820ebd077e2b90c4df007bebf344cd394098a13c563957d0afc83ea47" dependencies = [ - "thiserror-impl 2.0.3", + "thiserror-impl 2.0.6", ] [[package]] @@ -1675,9 +1675,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.3" +version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +checksum = "d65750cab40f4ff1929fb1ba509e9914eb756131cef4210da8d5d700d26f6312" dependencies = [ "proc-macro2", "quote", @@ -1982,12 +1982,12 @@ dependencies = [ [[package]] name = "winx" -version = "0.36.3" +version = "0.36.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9643b83820c0cd246ecabe5fa454dd04ba4fa67996369466d0747472d337346" +checksum = "3f3fd376f71958b862e7afb20cfe5a22830e1963462f3a17f49d82a6c1d1f42d" dependencies = [ "bitflags", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -2026,11 +2026,11 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.11" +version = "0.8.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cce3b5629d87654b53a49002acc2ce64aa5aa7255f5c718374a37ac7fd98c218" +checksum = "67914ab451f3bfd2e69e5e9d2ef3858484e7074d63f204fd166ec391b54de21d" dependencies = [ - "zerocopy-derive 0.8.11", + "zerocopy-derive 0.8.13", ] [[package]] @@ -2046,9 +2046,9 @@ dependencies = [ [[package]] name = "zerocopy-derive" -version = "0.8.11" +version = "0.8.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74a82c26c3986af2623ec9eb890ff4aa19c006e30a1133dc9bd1830ec1612e20" +checksum = "7988d73a4303ca289df03316bc490e934accf371af6bc745393cf3c2c5c4f25d" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 60c5b43..86d13c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,11 @@ version = "3.9.0" license = "GPL-3.0-only" edition = "2021" repository = "https://github.com/chenxiaolong/avbroot" + +[workspace.lints.clippy] +cast_lossless = "deny" +missing_fields_in_debug = "warn" +redundant_clone = "deny" + +[workspace.lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] } diff --git a/avbroot/Cargo.toml b/avbroot/Cargo.toml index 8eed085..372b016 100644 --- a/avbroot/Cargo.toml +++ b/avbroot/Cargo.toml @@ -86,5 +86,5 @@ assert_matches = "1.5.0" [features] static = ["bzip2/static", "liblzma/static"] -[lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] } +[lints] +workspace = true diff --git a/avbroot/src/cli/avb.rs b/avbroot/src/cli/avb.rs index aea0084..8d2b51a 100644 --- a/avbroot/src/cli/avb.rs +++ b/avbroot/src/cli/avb.rs @@ -340,7 +340,13 @@ fn sign_or_clear(info: &mut AvbInfo, orig_header: &Header, key_group: &KeyGroup) } let originally_signed = !info.header.signature.is_empty(); - let mut sign_action = if originally_signed && &info.header != orig_header { + let sign_action = if key_group.force { + if key_group.key.is_some() { + SignAction::Sign + } else { + SignAction::Clear + } + } else if originally_signed && &info.header != orig_header { SignAction::Sign } else { // If the original image was signed, we can preserve the existing @@ -349,14 +355,6 @@ fn sign_or_clear(info: &mut AvbInfo, orig_header: &Header, key_group: &KeyGroup) SignAction::None }; - if key_group.force { - sign_action = if key_group.key.is_some() { - SignAction::Sign - } else { - SignAction::Clear - }; - } - match sign_action { SignAction::None => { if originally_signed { @@ -521,10 +519,7 @@ fn verify_and_repair( cancel_signal: &AtomicBool, ) -> Result<()> { let _span = debug_span!("image", name = name.unwrap_or_default()).entered(); - let suffix = match name { - Some(n) => format!(" for: {n}"), - None => String::new(), - }; + let suffix = name.map_or_else(String::new, |n| format!(" for: {n}")); match descriptor { AppendedDescriptorRef::HashTree(d) => { @@ -538,7 +533,7 @@ fn verify_and_repair( d.repair(&file, &file, cancel_signal) .with_context(|| format!("Failed to repair data{suffix}"))?; - d.verify(&file, cancel_signal).map(|_| { + d.verify(&file, cancel_signal).inspect(|()| { info!("Successfully repaired data{suffix}"); }) } diff --git a/avbroot/src/cli/lp.rs b/avbroot/src/cli/lp.rs index 9c871f7..dd240dd 100644 --- a/avbroot/src/cli/lp.rs +++ b/avbroot/src/cli/lp.rs @@ -129,22 +129,19 @@ fn split_extents(extents: &[Extent]) -> Vec { /// Use the CLI-specified slot or automatically select one if all slots are /// identical. fn get_slot_number(metadata: &Metadata, cli_slot: Option) -> Result { - match cli_slot { - Some(n) => { - let n = n as usize; - if n >= metadata.slots.len() { - bail!("Slot out of range: {n}"); - } - - Ok(n) + if let Some(n) = cli_slot { + let n = n as usize; + if n >= metadata.slots.len() { + bail!("Slot out of range: {n}"); } - None => { - if metadata.slots.windows(2).any(|w| w[0] != w[1]) { - bail!("A slot must be specified because they are not all identical"); - } - Ok(0) + Ok(n) + } else { + if metadata.slots.windows(2).any(|w| w[0] != w[1]) { + bail!("A slot must be specified because they are not all identical"); } + + Ok(0) } } diff --git a/avbroot/src/cli/ota.rs b/avbroot/src/cli/ota.rs index 5cf463f..7542be6 100644 --- a/avbroot/src/cli/ota.rs +++ b/avbroot/src/cli/ota.rs @@ -8,7 +8,6 @@ use std::{ fmt::Display, fs::{self, File}, io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write}, - mem, ops::Range, path::{Path, PathBuf}, sync::{atomic::AtomicBool, Mutex}, @@ -193,7 +192,7 @@ fn open_input_files( fn patch_boot_images<'a, 'b: 'a>( required_images: &'b RequiredImages, input_files: &mut HashMap, - boot_patchers: Vec>, + boot_patchers: &[Box], key_avb: &RsaSigningKey, cancel_signal: &AtomicBool, ) -> Result<()> { @@ -219,7 +218,7 @@ fn patch_boot_images<'a, 'b: 'a>( WriteSeekReopen::reopen_boxed(&input_file.file) }, key_avb, - &boot_patchers, + boot_patchers, cancel_signal, ) .with_context(|| { @@ -348,7 +347,7 @@ fn ensure_partitions_protected( /// determine the order to patch the vbmeta images so that it can be done in a /// single pass. fn get_vbmeta_patch_order( - images: &mut HashMap, + images: &HashMap, vbmeta_headers: &HashMap, ) -> Result)>> { let mut dep_graph = HashMap::<&str, HashSet>::new(); @@ -722,7 +721,7 @@ fn patch_ota_payload( payload: &(dyn ReadSeekReopen + Sync), writer: impl Write, external_images: &HashMap, - boot_patchers: Vec>, + boot_patchers: &[Box], clear_vbmeta_flags: bool, key_avb: &RsaSigningKey, key_ota: &RsaSigningKey, @@ -794,7 +793,7 @@ fn patch_ota_payload( ensure_partitions_protected(&required_images, &vbmeta_headers)?; - let mut vbmeta_order = get_vbmeta_patch_order(&mut input_files, &vbmeta_headers)?; + let mut vbmeta_order = get_vbmeta_patch_order(&input_files, &vbmeta_headers)?; info!( "Patching vbmeta images: {}", @@ -914,7 +913,7 @@ fn patch_ota_zip( zip_reader: &mut ZipArchive, mut zip_writer: &mut ZipWriter, external_images: &HashMap, - mut boot_patchers: Vec>, + boot_patchers: &[Box], clear_vbmeta_flags: bool, zip_mode: ZipMode, key_avb: &RsaSigningKey, @@ -1036,8 +1035,7 @@ fn patch_ota_zip( &payload_reader, &mut writer, external_images, - // There's only one payload in the OTA. - mem::take(&mut boot_patchers), + boot_patchers, clear_vbmeta_flags, key_avb, key_ota, @@ -1347,7 +1345,7 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<() &mut zip_reader, &mut zip_writer, &external_images, - boot_patchers, + &boot_patchers, cli.clear_vbmeta_flags, cli.zip_mode, &key_avb, @@ -1633,7 +1631,7 @@ pub fn verify_subcommand(cli: &VerifyCli, cancel_signal: &AtomicBool) -> Result< ); } else if let Some(p) = &cli.cert_ota { let verify_cert = crypto::read_pem_cert_file(p) - .with_context(|| format!("Failed to load certificate: {:?}", p))?; + .with_context(|| format!("Failed to load certificate: {p:?}"))?; if embedded_cert != verify_cert { bail!("OTA has a valid signature, but was not signed with: {p:?}"); diff --git a/avbroot/src/cli/sparse.rs b/avbroot/src/cli/sparse.rs index ca6d370..7a44cc7 100644 --- a/avbroot/src/cli/sparse.rs +++ b/avbroot/src/cli/sparse.rs @@ -28,7 +28,7 @@ use crate::{ struct CompactView<'a, T>(&'a [T]); -impl<'a, T: fmt::Debug> fmt::Debug for CompactView<'a, T> { +impl fmt::Debug for CompactView<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut list = f.debug_list(); @@ -110,7 +110,7 @@ fn split_chunks(chunks: &[Chunk], block_size: u32) -> Vec { #[cfg(any(target_os = "linux", target_os = "android"))] fn find_allocated_regions( path: &Path, - reader: &mut File, + reader: &File, cancel_signal: &AtomicBool, ) -> Result>> { use rustix::{fs::SeekFrom, io::Errno}; @@ -122,13 +122,13 @@ fn find_allocated_regions( loop { stream::check_cancel(cancel_signal)?; - start = match rustix::fs::seek(&*reader, SeekFrom::Data(end as i64)) { + start = match rustix::fs::seek(reader, SeekFrom::Data(end as i64)) { Ok(offset) => offset, Err(e) if e == Errno::NXIO => break, Err(e) => return Err(e).with_context(|| format!("Failed to seek to data: {path:?}")), }; - end = rustix::fs::seek(&*reader, SeekFrom::Hole(start as i64)) + end = rustix::fs::seek(reader, SeekFrom::Hole(start as i64)) .with_context(|| format!("Failed to seek to hole: {path:?}"))?; result.push(start..end); @@ -375,7 +375,7 @@ fn pack_subcommand( } else { #[cfg(any(target_os = "linux", target_os = "android"))] { - let regions = find_allocated_regions(&cli.input, &mut reader, cancel_signal)?; + let regions = find_allocated_regions(&cli.input, &reader, cancel_signal)?; (regions, false) } diff --git a/avbroot/src/crypto.rs b/avbroot/src/crypto.rs index fddaf48..804572e 100644 --- a/avbroot/src/crypto.rs +++ b/avbroot/src/crypto.rs @@ -235,8 +235,8 @@ impl RsaSigningKey { /// Get the public key portion of the signing key. pub fn to_public_key(&self) -> RsaPublicKey { match self { - RsaSigningKey::Internal(key) => key.to_public_key(), - RsaSigningKey::External { public_key, .. } => public_key.clone(), + Self::Internal(key) => key.to_public_key(), + Self::External { public_key, .. } => public_key.clone(), } } diff --git a/avbroot/src/escape.rs b/avbroot/src/escape.rs index 6bf0b06..52cbeac 100644 --- a/avbroot/src/escape.rs +++ b/avbroot/src/escape.rs @@ -66,7 +66,7 @@ where { struct EscapedStrVisitor(PhantomData); - impl<'de, T> Visitor<'de> for EscapedStrVisitor + impl Visitor<'_> for EscapedStrVisitor where T: FromEscaped, ::Error: fmt::Display, diff --git a/avbroot/src/format/avb.rs b/avbroot/src/format/avb.rs index 898f0ed..216c92e 100644 --- a/avbroot/src/format/avb.rs +++ b/avbroot/src/format/avb.rs @@ -534,27 +534,24 @@ impl HashTreeDescriptor { let parity: u8 = util::try_cast(self.fec_num_roots) .map_err(|e| Error::IntOutOfBounds("HashTreeDescriptor::fec_num_roots", e))?; - let fec_data = match ranges { - Some(r) => { - let mut r_with_hash_tree = r.to_vec(); - r_with_hash_tree.push(self.tree_offset..self.tree_offset + tree_size); + let fec_data = if let Some(r) = ranges { + let mut r_with_hash_tree = r.to_vec(); + r_with_hash_tree.push(self.tree_offset..self.tree_offset + tree_size); - let (fec, fec_size) = self.get_fec()?; + let (fec, fec_size) = self.get_fec()?; - let mut reader = input.reopen_boxed()?; - reader.seek(SeekFrom::Start(self.fec_offset))?; + let mut reader = input.reopen_boxed()?; + reader.seek(SeekFrom::Start(self.fec_offset))?; - let mut fec_data = reader.read_vec_exact(fec_size)?; + let mut fec_data = reader.read_vec_exact(fec_size)?; - fec.update(input, &r_with_hash_tree, &mut fec_data, cancel_signal)?; + fec.update(input, &r_with_hash_tree, &mut fec_data, cancel_signal)?; - fec_data - } - None => { - // The FEC covers the hash tree as well. - let fec = Fec::new(self.image_size + tree_size, self.data_block_size, parity)?; - fec.generate(input, cancel_signal)? - } + fec_data + } else { + // The FEC covers the hash tree as well. + let fec = Fec::new(self.image_size + tree_size, self.data_block_size, parity)?; + fec.generate(input, cancel_signal)? }; // Already seeked to FEC. diff --git a/avbroot/src/format/cpio.rs b/avbroot/src/format/cpio.rs index 8854e12..de74ae5 100644 --- a/avbroot/src/format/cpio.rs +++ b/avbroot/src/format/cpio.rs @@ -218,7 +218,7 @@ impl CpioEntryData { } fn is_size(&self) -> bool { - matches!(self, CpioEntryData::Size(_)) + matches!(self, Self::Size(_)) } } @@ -680,7 +680,7 @@ pub fn load( stream::check_cancel(cancel_signal)?; if entry.file_type != CpioEntryType::Directory && entry.nlink > 1 { - return Err(Error::HardLinksNotSupported(entry.path.clone())); + return Err(Error::HardLinksNotSupported(entry.path)); } if let CpioEntryData::Size(s) = entry.data { diff --git a/avbroot/src/format/fec.rs b/avbroot/src/format/fec.rs index e55509d..9029b15 100644 --- a/avbroot/src/format/fec.rs +++ b/avbroot/src/format/fec.rs @@ -97,7 +97,7 @@ impl Codeword { &mut self.data[..usize::from(self.rs_k)] } - fn parity(&mut self) -> &[u8] { + fn parity(&self) -> &[u8] { &self.data[usize::from(self.rs_k)..] } diff --git a/avbroot/src/format/hashtree.rs b/avbroot/src/format/hashtree.rs index b5b6b0d..9e98d8a 100644 --- a/avbroot/src/format/hashtree.rs +++ b/avbroot/src/format/hashtree.rs @@ -189,7 +189,7 @@ impl HashTree { cancel_signal: &AtomicBool, ) -> io::Result<()> { assert!( - size > self.block_size as u64, + size > u64::from(self.block_size), "Images smaller than block size must use a normal hash", ); @@ -329,7 +329,7 @@ impl HashTree { cancel_signal: &AtomicBool, ) -> Result<(Vec, Vec)> { let offsets = self.compute_level_offsets(image_size)?; - let hash_tree_size = offsets.first().map(|r| r.end).unwrap_or(0); + let hash_tree_size = offsets.first().map_or(0, |r| r.end); let mut hash_tree_data = vec![0u8; hash_tree_size]; let root_digest = self.calculate( @@ -355,7 +355,7 @@ impl HashTree { cancel_signal: &AtomicBool, ) -> Result> { let offsets = self.compute_level_offsets(image_size)?; - let hash_tree_size = offsets.first().map(|r| r.end).unwrap_or(0); + let hash_tree_size = offsets.first().map_or(0, |r| r.end); if hash_tree_data.len() != hash_tree_size { return Err(Error::InvalidHashTreeSize { input: image_size, @@ -384,7 +384,7 @@ impl HashTree { cancel_signal: &AtomicBool, ) -> Result<()> { let offsets = self.compute_level_offsets(image_size)?; - let hash_tree_size = offsets.first().map(|r| r.end).unwrap_or(0); + let hash_tree_size = offsets.first().map_or(0, |r| r.end); if hash_tree_data.len() != hash_tree_size { return Err(Error::InvalidHashTreeSize { input: image_size, @@ -468,6 +468,7 @@ pub struct HashTreeImage { impl fmt::Debug for HashTreeImage { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("HashTreeImage") + .field("image_size", &self.image_size) .field("block_size", &self.block_size) .field("algorithm", &self.algorithm) .field("salt", &hex::encode(&self.salt)) diff --git a/avbroot/src/format/lp.rs b/avbroot/src/format/lp.rs index 38dd395..34ff5e9 100644 --- a/avbroot/src/format/lp.rs +++ b/avbroot/src/format/lp.rs @@ -586,10 +586,10 @@ impl fmt::Debug for PartitionName { impl PartitionName { fn split(&self) -> (&[u8], &[u8]) { - match self.0.iter().position(|b| *b == 0) { - Some(i) => self.0.split_at(i), - None => (&self.0, &[]), - } + self.0 + .iter() + .position(|b| *b == 0) + .map_or((&self.0, &[]), |i| self.0.split_at(i)) } fn validate(&self) -> Result<()> { @@ -1071,18 +1071,15 @@ impl RawMetadata { let mut geometry = RawGeometry::ref_from_prefix(&buf).unwrap().0; - match geometry.validate() { - Ok(_) => { - // Skip the backup copy. - reader.read_discard_exact(GEOMETRY_SIZE.into())?; - } - Err(_) => { - // Try to parse the backup copy. - reader.read_exact(&mut buf)?; + if geometry.validate().is_ok() { + // Skip the backup copy. + reader.read_discard_exact(GEOMETRY_SIZE.into())?; + } else { + // Try to parse the backup copy. + reader.read_exact(&mut buf)?; - geometry = RawGeometry::ref_from_prefix(&buf).unwrap().0; - geometry.validate()?; - } + geometry = RawGeometry::ref_from_prefix(&buf).unwrap().0; + geometry.validate()?; } geometry diff --git a/avbroot/src/format/ota.rs b/avbroot/src/format/ota.rs index 3460acf..627bf33 100644 --- a/avbroot/src/format/ota.rs +++ b/avbroot/src/format/ota.rs @@ -135,7 +135,7 @@ pub fn parse_legacy_metadata(data: &str) -> Result { } "ota-wipe" => metadata.wipe = parse_yes()?, "ota-retrofit-dynamic-partitions" => { - metadata.retrofit_dynamic_partitions = parse_yes()? + metadata.retrofit_dynamic_partitions = parse_yes()?; } "ota-downgrade" => metadata.downgrade = parse_yes()?, "ota-required-cache" => { @@ -190,7 +190,7 @@ pub fn parse_legacy_metadata(data: &str) -> Result { /// Generate the legacy plain-text and modern protobuf serializations of the /// given metadata instance. -fn serialize_metadata(metadata: &OtaMetadata) -> Result<(String, Vec)> { +fn serialize_metadata(metadata: &OtaMetadata) -> (String, Vec) { use std::fmt::Write; let mut pairs = BTreeMap::::new(); @@ -254,7 +254,7 @@ fn serialize_metadata(metadata: &OtaMetadata) -> Result<(String, Vec)> { }); let modern_metadata = metadata.encode_to_vec(); - Ok((legacy_metadata, modern_metadata)) + (legacy_metadata, modern_metadata) } #[derive(Clone, Debug)] @@ -426,7 +426,7 @@ pub fn add_metadata( // Add the placeholders to a temporary zip to compute final property files. let (temp_legacy_offset, temp_modern_offset) = { - let (legacy_raw, modern_raw) = serialize_metadata(&metadata)?; + let (legacy_raw, modern_raw) = serialize_metadata(&metadata); let raw_writer = Cursor::new(Vec::new()); let mut writer = match zip_mode { ZipMode::Streaming => ZipWriter::new_streaming(raw_writer), @@ -462,7 +462,7 @@ pub fn add_metadata( // Add the final metadata files to the real zip. { - let (legacy_raw, modern_raw) = serialize_metadata(&metadata)?; + let (legacy_raw, modern_raw) = serialize_metadata(&metadata); zip_writer.start_file_with_extra_data(PATH_METADATA, options)?; let legacy_offset = zip_writer.end_extra_data()?; diff --git a/avbroot/src/format/payload.rs b/avbroot/src/format/payload.rs index cfea702..037dc5b 100644 --- a/avbroot/src/format/payload.rs +++ b/avbroot/src/format/payload.rs @@ -218,7 +218,7 @@ fn verify_digest(digest: &[u8], signatures: &Signatures, cert: &Certificate) -> let without_padding = &data[..size]; match public_key.verify_sig(SignatureAlgorithm::Sha256WithRsa, digest, without_padding) { - Ok(_) => return Ok(()), + Ok(()) => return Ok(()), Err(e) => last_error = Some(e), } } @@ -902,7 +902,7 @@ impl VabcAlgo { } } - fn compressed_size(&self, mut raw_data: &[u8], block_size: u32) -> u64 { + fn compressed_size(self, mut raw_data: &[u8], block_size: u32) -> u64 { let mut total = 0; while !raw_data.is_empty() { @@ -1011,11 +1011,8 @@ pub fn compress_image( .map( |(raw_offset, raw_data)| -> Result<(Vec, InstallOperation, u64)> { let (data, digest_compressed) = compress_chunk(&raw_data, cancel_signal)?; - let cow_size = if let Some(algo) = vabc_algo { - algo.compressed_size(&raw_data, block_size) - } else { - 0 - }; + let cow_size = + vabc_algo.map_or(0, |a| a.compressed_size(&raw_data, block_size)); let extent = Extent { start_block: Some(raw_offset / u64::from(block_size)), diff --git a/avbroot/src/main.rs b/avbroot/src/main.rs index 03891bb..53d7779 100644 --- a/avbroot/src/main.rs +++ b/avbroot/src/main.rs @@ -26,7 +26,7 @@ fn main() -> ExitCode { } match avbroot::cli::args::main(&LOGGING_INITIALIZED, &cancel_signal) { - Ok(_) => ExitCode::SUCCESS, + Ok(()) => ExitCode::SUCCESS, Err(e) => { if LOGGING_INITIALIZED.load(Ordering::SeqCst) { error!("{e:?}"); diff --git a/avbroot/src/octal.rs b/avbroot/src/octal.rs index 444a6bc..a17825b 100644 --- a/avbroot/src/octal.rs +++ b/avbroot/src/octal.rs @@ -28,7 +28,7 @@ where { struct OctalStrVisitor(PhantomData); - impl<'de, T> Visitor<'de> for OctalStrVisitor + impl Visitor<'_> for OctalStrVisitor where T: PrimInt, ::FromStrRadixErr: fmt::Display, diff --git a/avbroot/src/patch/boot.rs b/avbroot/src/patch/boot.rs index 7bb88f9..afa2160 100644 --- a/avbroot/src/patch/boot.rs +++ b/avbroot/src/patch/boot.rs @@ -610,7 +610,6 @@ impl OtaCertPatcher { } fn patch_ramdisk( - &self, ramdisk: &mut Vec, zip: &[u8], cancel_signal: &AtomicBool, @@ -681,7 +680,7 @@ impl BootImagePatch for OtaCertPatcher { continue; } - if self.patch_ramdisk(ramdisk, &new_zip, cancel_signal)? { + if Self::patch_ramdisk(ramdisk, &new_zip, cancel_signal)? { return Ok(()); } } @@ -794,7 +793,7 @@ impl BootImagePatch for DsuPubKeyPatcher { // For builds that don't trust any DSU keys, pick the first boot // image that contains a first stage ramdisk directory. if !first_stage_targets.is_empty() { - first_stage_targets.sort(); + first_stage_targets.sort_unstable(); first_stage_targets.resize(1, ""); } @@ -1228,5 +1227,5 @@ pub fn patch_boot_images<'a>( }) .collect::>()?; - Ok(groups.keys().cloned().collect()) + Ok(groups.keys().copied().collect()) } diff --git a/avbroot/src/protobuf.rs b/avbroot/src/protobuf.rs index a8c858f..11d6e5b 100644 --- a/avbroot/src/protobuf.rs +++ b/avbroot/src/protobuf.rs @@ -1,3 +1,6 @@ +#![allow(clippy::nursery)] +#![allow(clippy::pedantic)] + pub mod build { pub mod tools { pub mod releasetools { diff --git a/avbroot/src/stream.rs b/avbroot/src/stream.rs index 535f73c..3448dbf 100644 --- a/avbroot/src/stream.rs +++ b/avbroot/src/stream.rs @@ -153,13 +153,13 @@ pub trait Reopen: Sized { impl Reopen for BufReader { fn reopen(&self) -> io::Result { - Ok(BufReader::new(self.get_ref().reopen()?)) + Ok(Self::new(self.get_ref().reopen()?)) } } impl Reopen for BufWriter { fn reopen(&self) -> io::Result { - Ok(BufWriter::new(self.get_ref().reopen()?)) + Ok(Self::new(self.get_ref().reopen()?)) } } @@ -499,9 +499,7 @@ pub struct SharedCursor { impl SharedCursor { pub fn new() -> Self { - Self { - ..Default::default() - } + Self::default() } } diff --git a/avbroot/src/util.rs b/avbroot/src/util.rs index cdf2c5e..e2db1ad 100644 --- a/avbroot/src/util.rs +++ b/avbroot/src/util.rs @@ -351,23 +351,23 @@ mod tests { #[test] fn test_ranges_overlaps() { - assert_eq!(ranges_overlaps(&[0..4], &(0..0)), false); - assert_eq!(ranges_overlaps(&[0..4], &(0..4)), true); - assert_eq!(ranges_overlaps(&[0..4], &(1..4)), true); - assert_eq!(ranges_overlaps(&[0..4], &(0..3)), true); - assert_eq!(ranges_overlaps(&[0..4], &(4..5)), false); - assert_eq!(ranges_overlaps(&[5..8], &(5..9)), true); - assert_eq!(ranges_overlaps(&[5..8], &(4..8)), true); - assert_eq!(ranges_overlaps(&[5..8], &(4..9)), true); - assert_eq!(ranges_overlaps(&[0..4, 5..8], &(4..5)), true); - assert_eq!(ranges_overlaps(&[0..4, 5..8], &(0..9)), true); + assert!(!ranges_overlaps(&[0..4], &(0..0))); + assert!(ranges_overlaps(&[0..4], &(0..4))); + assert!(ranges_overlaps(&[0..4], &(1..4))); + assert!(ranges_overlaps(&[0..4], &(0..3))); + assert!(!ranges_overlaps(&[0..4], &(4..5))); + assert!(ranges_overlaps(&[5..8], &(5..9))); + assert!(ranges_overlaps(&[5..8], &(4..8))); + assert!(ranges_overlaps(&[5..8], &(4..9))); + assert!(ranges_overlaps(&[0..4, 5..8], &(4..5))); + assert!(ranges_overlaps(&[0..4, 5..8], &(0..9))); } #[test] fn test_ranges_contains() { - assert_eq!(ranges_contains(&[0..4], &0), true); - assert_eq!(ranges_contains(&[0..4], &4), false); - assert_eq!(ranges_contains(&[0..4, 5..8], &4), false); - assert_eq!(ranges_contains(&[0..4, 5..8], &6), true); + assert!(ranges_contains(&[0..4], &0)); + assert!(!ranges_contains(&[0..4], &4)); + assert!(!ranges_contains(&[0..4, 5..8], &4)); + assert!(ranges_contains(&[0..4, 5..8], &6)); } } diff --git a/e2e/Cargo.toml b/e2e/Cargo.toml index 1fbe363..5d28201 100644 --- a/e2e/Cargo.toml +++ b/e2e/Cargo.toml @@ -32,3 +32,6 @@ default-features = false [features] static = ["avbroot/static"] + +[lints] +workspace = true diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index c4884af..cfc59ed 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -16,3 +16,6 @@ publish = false [target.'cfg(unix)'.dependencies] avbroot = { path = "../avbroot" } honggfuzz = "0.5.55" + +[lints] +workspace = true diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index e9638cf..fa311ed 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -13,3 +13,6 @@ anyhow = "1.0.75" clap = { version = "4.4.1", features = ["derive"] } regex = { version = "1.9.4", default-features = false, features = ["perf", "std"] } toml_edit = "0.22.9" + +[lints] +workspace = true