Skip to content

Commit 35fab17

Browse files
authored
Merge pull request #413 from Dstack-TEE/no-panic
Improve coding style
2 parents 2ccc6a7 + ad4a840 commit 35fab17

File tree

42 files changed

+391
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+391
-92
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
components: clippy, rustfmt
2626

2727
- name: Run Clippy
28-
run: cargo clippy -- -D warnings --allow unused_variables
28+
run: cargo clippy -- -D warnings -D clippy::expect_used -D clippy::unwrap_used --allow unused_variables
2929

3030
- name: Cargo fmt check
3131
run: cargo fmt --check --all

Cargo.lock

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ size-parser = { path = "size-parser" }
8686

8787
# Core dependencies
8888
anyhow = { version = "1.0.97", default-features = false }
89+
or-panic = { version = "1.0", default-features = false }
8990
chrono = "0.4.40"
9091
clap = { version = "4.5.32", features = ["derive", "string"] }
9192
dashmap = "6.1.0"

certbot/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ tokio = { workspace = true, features = ["full"] }
2424
toml_edit.workspace = true
2525
tracing-subscriber.workspace = true
2626
rustls.workspace = true
27+
or-panic.workspace = true

certbot/cli/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use certbot::{CertBotConfig, WorkDir};
1010
use clap::Parser;
1111
use documented::DocumentedFields;
1212
use fs_err as fs;
13+
use or_panic::ResultOrPanic;
1314
use serde::{Deserialize, Serialize};
1415
use toml_edit::ser::to_document;
1516

@@ -166,7 +167,7 @@ async fn main() -> Result<()> {
166167
}
167168
rustls::crypto::ring::default_provider()
168169
.install_default()
169-
.expect("Failed to install default crypto provider");
170+
.or_panic("Failed to install default crypto provider");
170171

171172
let args = Args::parse();
172173
match args.command {

ct_monitor/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ impl Monitor {
140140

141141
fn validate_domain(domain: &str) -> Result<()> {
142142
let domain_regex =
143-
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$").unwrap();
143+
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$")
144+
.context("invalid regex")?;
144145
if !domain_regex.is_match(domain) {
145146
bail!("invalid domain name");
146147
}

dstack-mr/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ hex-literal.workspace = true
2424
fs-err.workspace = true
2525
bon.workspace = true
2626
log.workspace = true
27+
scale.workspace = true
28+
29+
[dev-dependencies]
30+
dstack-types.workspace = true
31+
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] }
32+
flate2 = "1.0"
33+
tar = "0.4"

dstack-mr/cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn main() -> Result<()> {
118118
.context("Failed to measure machine configuration")?;
119119

120120
if config.json {
121-
println!("{}", serde_json::to_string_pretty(&measurements).unwrap());
121+
println!("{}", serde_json::to_string_pretty(&measurements)?);
122122
} else {
123123
println!("Machine measurements:");
124124
println!("MRTD: {}", hex::encode(measurements.mrtd));

dstack-mr/src/acpi.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use anyhow::{bail, Context, Result};
99
use log::debug;
10+
use scale::Decode;
1011

1112
use crate::Machine;
1213

@@ -392,6 +393,13 @@ fn qemu_loader_append(data: &mut Vec<u8>, cmd: LoaderCmd) {
392393
}
393394
}
394395

396+
/// ACPI table header (first 8 bytes of every ACPI table)
397+
#[derive(Debug, Decode)]
398+
struct AcpiTableHeader {
399+
signature: [u8; 4],
400+
length: u32,
401+
}
402+
395403
/// Searches for an ACPI table with the given signature and returns its offset,
396404
/// checksum offset, and length.
397405
fn find_acpi_table(tables: &[u8], signature: &str) -> Result<(u32, u32, u32)> {
@@ -407,22 +415,21 @@ fn find_acpi_table(tables: &[u8], signature: &str) -> Result<(u32, u32, u32)> {
407415
bail!("Table not found: {signature}");
408416
}
409417

410-
let tbl_sig = &tables[offset..offset + 4];
411-
let tbl_len_bytes: [u8; 4] = tables[offset + 4..offset + 8].try_into().unwrap();
412-
let tbl_len = u32::from_le_bytes(tbl_len_bytes) as usize;
418+
let header = AcpiTableHeader::decode(&mut &tables[offset..])
419+
.context("failed to decode ACPI table header")?;
413420

414-
if tbl_sig == sig_bytes {
421+
if header.signature == sig_bytes {
415422
// Found the table
416-
return Ok((offset as u32, (offset + 9) as u32, tbl_len as u32));
423+
return Ok((offset as u32, (offset + 9) as u32, header.length));
417424
}
418425

419-
if tbl_len == 0 {
426+
if header.length == 0 {
420427
// Invalid table length, stop searching
421-
bail!("Found table with zero length at offset {offset}");
428+
bail!("found table with zero length at offset {offset}");
422429
}
423430
// Move to the next table
424-
offset += tbl_len;
431+
offset += header.length as usize;
425432
}
426433

427-
bail!("Table not found: {signature}");
434+
bail!("table not found: {signature}");
428435
}

dstack-mr/src/kernel.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn patch_kernel(
129129

130130
let mut kd = kernel_data.to_vec();
131131

132-
let protocol = u16::from_le_bytes(kd[0x206..0x208].try_into().unwrap());
132+
let protocol = u16::from_le_bytes(kd[0x206..0x208].try_into().context("impossible failure")?);
133133

134134
let (real_addr, cmdline_addr) = if protocol < 0x200 || (kd[0x211] & 0x01) == 0 {
135135
(0x90000_u32, 0x9a000_u32)
@@ -158,14 +158,16 @@ fn patch_kernel(
158158
bail!("the kernel image is too old for ramdisk");
159159
}
160160
let mut initrd_max = if protocol >= 0x20c {
161-
let xlf = u16::from_le_bytes(kd[0x236..0x238].try_into().unwrap());
161+
let xlf =
162+
u16::from_le_bytes(kd[0x236..0x238].try_into().context("impossible failure")?);
162163
if (xlf & 0x40) != 0 {
163164
u32::MAX
164165
} else {
165166
0x37ffffff
166167
}
167168
} else if protocol >= 0x203 {
168-
let max = u32::from_le_bytes(kd[0x22c..0x230].try_into().unwrap());
169+
let max =
170+
u32::from_le_bytes(kd[0x22c..0x230].try_into().context("impossible failure")?);
169171
if max == 0 {
170172
0x37ffffff
171173
} else {

0 commit comments

Comments
 (0)