Skip to content

Commit 6a1cd93

Browse files
authored
Merge pull request #92 from hug-dev/unsafe-unwrap
Replace most panicking behaviours with Result
2 parents 482441e + 1309a17 commit 6a1cd93

File tree

15 files changed

+412
-252
lines changed

15 files changed

+412
-252
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ name = "parsec"
1111
path = "src/bin/main.rs"
1212

1313
[dependencies]
14-
parsec-interface = { git = "https://github.com/parallaxsecond/parsec-interface-rs", tag = "0.4.0" }
14+
parsec-interface = { git = "https://github.com/parallaxsecond/parsec-interface-rs", tag = "0.5.0" }
1515
rand = "0.7.2"
1616
base64 = "0.10.1"
1717
uuid = "0.7.4"
@@ -26,13 +26,13 @@ log = { version = "0.4.8", features = ["serde"] }
2626
pkcs11 = { version = "0.4.0", optional = true }
2727
picky-asn1-der = { version = "0.2.2", optional = true }
2828
picky-asn1 = { version = "0.2.1", optional = true }
29-
tss-esapi = { version = "1.0.1", optional = true }
29+
tss-esapi = { version = "2.0.0", optional = true }
3030
bincode = "1.1.4"
3131
structopt = "0.3.5"
3232
derivative = "1.0.3"
3333

3434
[dev-dependencies]
35-
parsec-client-test = { git = "https://github.com/parallaxsecond/parsec-client-test", tag = "0.1.10" }
35+
parsec-client-test = { git = "https://github.com/parallaxsecond/parsec-client-test", tag = "0.1.11" }
3636
num_cpus = "1.10.1"
3737

3838
[build-dependencies]

build.rs

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use cargo_toml::{Manifest, Value};
1616
use serde::Deserialize;
1717
use std::env;
18+
use std::io::{Error, ErrorKind, Result};
1819
use std::path::{Path, PathBuf};
1920

2021
const CONFIG_TABLE_NAME: &str = "config";
@@ -46,28 +47,34 @@ struct Toolchain {
4647
mbed_archiver: Option<String>,
4748
}
4849

49-
fn get_configuration_string(parsec_config: &Value, key: &str) -> String {
50-
let config_value = get_value_from_table(parsec_config, key);
50+
fn get_configuration_string(parsec_config: &Value, key: &str) -> Result<String> {
51+
let config_value = get_value_from_table(parsec_config, key)?;
5152
match config_value {
52-
Value::String(string) => string.clone(),
53-
_ => panic!("Cargo.toml does not contain configuration key: {}", key),
53+
Value::String(string) => Ok(string.clone()),
54+
_ => Err(Error::new(
55+
ErrorKind::InvalidInput,
56+
"Configuration key missing",
57+
)),
5458
}
5559
}
5660

57-
fn get_value_from_table<'a>(table: &'a Value, key: &str) -> &'a Value {
61+
fn get_value_from_table<'a>(table: &'a Value, key: &str) -> Result<&'a Value> {
5862
match table {
59-
Value::Table(table) => table.get(key).expect(&format!(
60-
"Config table does not contain configuration key: {}",
61-
key
63+
Value::Table(table) => table.get(key).ok_or_else(|| {
64+
println!("Config table does not contain configuration key: {}", key);
65+
Error::new(ErrorKind::InvalidInput, "Configuration key missing.")
66+
}),
67+
_ => Err(Error::new(
68+
ErrorKind::InvalidInput,
69+
"Value provided is not a TOML table",
6270
)),
63-
_ => panic!("Value provided is not a TOML table"),
6471
}
6572
}
6673

6774
// Get the Mbed Crypto version to branch on from Cargo.toml file. Use that and MbedConfig to pass
6875
// parameters to the setup_mbed_crypto.sh script which clones and builds Mbed Crypto and create
6976
// a static library.
70-
fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
77+
fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) -> Result<()> {
7178
let mut run_script = ::std::process::Command::new(SETUP_MBED_SCRIPT_PATH);
7279
run_script.arg(mbed_version).arg(
7380
mbed_config
@@ -80,7 +87,15 @@ fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
8087
let mbed_compiler;
8188
let mbed_archiver;
8289
if std::env::var("TARGET").unwrap() == "aarch64-unknown-linux-gnu" {
83-
toolchain = mbed_config.aarch64_unknown_linux_gnu.as_ref().unwrap();
90+
toolchain = mbed_config
91+
.aarch64_unknown_linux_gnu
92+
.as_ref()
93+
.ok_or_else(|| {
94+
Error::new(
95+
ErrorKind::InvalidInput,
96+
"The aarch64_unknown_linux_gnu subtable of mbed_config should exist",
97+
)
98+
})?;
8499
mbed_compiler = toolchain
85100
.mbed_compiler
86101
.clone()
@@ -90,7 +105,12 @@ fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
90105
.clone()
91106
.unwrap_or(DEFAULT_ARM64_MBED_ARCHIVER.to_string());
92107
} else {
93-
toolchain = mbed_config.native.as_ref().unwrap();
108+
toolchain = mbed_config.native.as_ref().ok_or_else(|| {
109+
Error::new(
110+
ErrorKind::InvalidInput,
111+
"The native subtable of mbed_config should exist",
112+
)
113+
})?;
94114
mbed_compiler = toolchain
95115
.mbed_compiler
96116
.clone()
@@ -106,14 +126,24 @@ fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
106126

107127
if !run_script
108128
.status()
109-
.expect("setup_mbed_crypto.sh script failed.")
129+
.or_else(|_| {
130+
Err(Error::new(
131+
ErrorKind::Other,
132+
"setup_mbed_crypto.sh script failed",
133+
))
134+
})?
110135
.success()
111136
{
112-
panic!("setup_mbed_crypto.sh returned an error status.");
137+
Err(Error::new(
138+
ErrorKind::Other,
139+
"setup_mbed_crypto.sh returned an error status.",
140+
))
141+
} else {
142+
Ok(())
113143
}
114144
}
115145

116-
fn generate_mbed_bindings(mbed_config: &MbedConfig, mbed_version: &str) {
146+
fn generate_mbed_bindings(mbed_config: &MbedConfig, mbed_version: &str) -> Result<()> {
117147
let mbed_include_dir = mbed_config
118148
.mbed_path
119149
.clone()
@@ -131,50 +161,75 @@ fn generate_mbed_bindings(mbed_config: &MbedConfig, mbed_version: &str) {
131161
.header(header)
132162
.generate_comments(false)
133163
.generate()
134-
.expect("Unable to generate bindings to mbed crypto");
164+
.or_else(|_| {
165+
Err(Error::new(
166+
ErrorKind::Other,
167+
"Unable to generate bindings to mbed crypto",
168+
))
169+
})?;
135170

136171
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
137-
bindings
138-
.write_to_file(out_path.join("psa_crypto_bindings.rs"))
139-
.expect(&format!("Couldn't write bindings to {:?}!", out_path));
172+
bindings.write_to_file(out_path.join("psa_crypto_bindings.rs"))
140173
}
141174

142175
// Get the compiler, the archiver and the location where to clone the Mbed Crypto repository.
143-
fn parse_config_file() -> Configuration {
144-
let config_str = ::std::fs::read_to_string(Path::new(BUILD_CONFIG_FILE_PATH))
145-
.expect("Could not read configuration file.");
146-
toml::from_str(&config_str).expect("Could not parse build configuration file.")
176+
fn parse_config_file() -> Result<Configuration> {
177+
let config_str = ::std::fs::read_to_string(Path::new(BUILD_CONFIG_FILE_PATH))?;
178+
Ok(toml::from_str(&config_str).or_else(|e| {
179+
println!("Error parsing build configuration file ({}).", e);
180+
Err(Error::new(
181+
ErrorKind::InvalidInput,
182+
"Could not parse build configuration file.",
183+
))
184+
})?)
147185
}
148186

149-
fn main() {
187+
fn main() -> Result<()> {
150188
// Parsing build-conf.toml
151-
let config = parse_config_file();
189+
let config = parse_config_file()?;
152190

153191
// Parsing Cargo.toml
154192
let toml_path = std::path::Path::new("./Cargo.toml");
155193
if !toml_path.exists() {
156-
panic!("Could not find Cargo.toml.");
194+
return Err(Error::new(
195+
ErrorKind::InvalidInput,
196+
"Could not find Cargo.toml.",
197+
));
157198
}
158-
let manifest = Manifest::from_path(&toml_path).expect("Could not parse Cargo.toml.");
159-
160-
let package = manifest
161-
.package
162-
.expect("Cargo.toml does not contain package information.");
163-
let metadata = package
164-
.metadata
165-
.expect("Cargo.toml does not contain package metadata.");
166-
let parsec_config = get_value_from_table(&metadata, CONFIG_TABLE_NAME);
199+
let manifest = Manifest::from_path(&toml_path).or_else(|e| {
200+
println!("Error parsing Cargo.toml ({}).", e);
201+
Err(Error::new(
202+
ErrorKind::InvalidInput,
203+
"Could not parse Cargo.toml.",
204+
))
205+
})?;
206+
207+
let package = manifest.package.ok_or_else(|| {
208+
Error::new(
209+
ErrorKind::InvalidInput,
210+
"Cargo.toml does not contain package information.",
211+
)
212+
})?;
213+
let metadata = package.metadata.ok_or_else(|| {
214+
Error::new(
215+
ErrorKind::InvalidInput,
216+
"Cargo.toml does not contain package metadata.",
217+
)
218+
})?;
219+
let parsec_config = get_value_from_table(&metadata, CONFIG_TABLE_NAME)?;
167220

168221
if cfg!(feature = "mbed-crypto-provider") {
169-
let mbed_config = config.mbed_config.expect(&format!(
170-
"Could not find mbed_config table in the {} file.",
171-
BUILD_CONFIG_FILE_PATH
172-
));
222+
let mbed_config = config.mbed_config.ok_or_else(|| {
223+
Error::new(
224+
ErrorKind::InvalidInput,
225+
"Could not find mbed_config table in the config file.",
226+
)
227+
})?;
173228

174-
let mbed_version = get_configuration_string(&parsec_config, MBED_CRYPTO_VERSION_KEY);
229+
let mbed_version = get_configuration_string(&parsec_config, MBED_CRYPTO_VERSION_KEY)?;
175230

176-
setup_mbed_crypto(&mbed_config, &mbed_version);
177-
generate_mbed_bindings(&mbed_config, &mbed_version);
231+
setup_mbed_crypto(&mbed_config, &mbed_version)?;
232+
generate_mbed_bindings(&mbed_config, &mbed_version)?;
178233

179234
// Request rustc to link the Mbed Crypto static library
180235
println!(
@@ -186,4 +241,6 @@ fn main() {
186241
);
187242
println!("cargo:rustc-link-lib=static=mbedcrypto");
188243
}
244+
245+
Ok(())
189246
}

0 commit comments

Comments
 (0)