Skip to content

Commit 3643741

Browse files
authored
Merge pull request #10703 from holtrop-wolfssl/rust-validate-prefix-env-var
Rust wrapper: validate WOLFSSL_PREFIX in build.rs
2 parents a66b87b + d523e46 commit 3643741

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

wrapper/rust/wolfssl-wolfcrypt/build.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,30 @@ fn wolfssl_repo_lib_dir() -> Result<String> {
3737
Ok(format!("{}/src/.libs", wolfssl_repo_base_dir()?))
3838
}
3939

40+
fn wolfssl_user_prefix() -> Option<String> {
41+
match env::var("WOLFSSL_PREFIX") {
42+
Ok(prefix) => {
43+
if !prefix.is_empty() && !prefix.contains('\n') {
44+
Some(prefix)
45+
} else {
46+
println!("cargo:warning=ignoring WOLFSSL_PREFIX");
47+
None
48+
}
49+
}
50+
Err(_) => None,
51+
}
52+
}
53+
4054
/// Returns the include directory for wolfssl headers.
4155
///
4256
/// If `WOLFSSL_PREFIX` is set, returns `{WOLFSSL_PREFIX}/include`.
4357
/// Otherwise falls back to the repo root if it exists (for in-tree host builds).
4458
fn wolfssl_include_dir() -> Result<Option<String>> {
45-
if let Ok(prefix) = env::var("WOLFSSL_PREFIX") {
59+
if let Some(prefix) = wolfssl_user_prefix() {
4660
let include_dir = format!("{}/include", prefix);
4761
let wolfssl_dir = Path::new(&include_dir).join("wolfssl");
4862
if !wolfssl_dir.is_dir() {
49-
println!("cargo:warning=WOLFSSL_PREFIX is set but {} does not exist", wolfssl_dir.display());
63+
println!("cargo:warning=WOLFSSL_PREFIX is set but {} is not a directory", wolfssl_dir.display());
5064
return Ok(None);
5165
}
5266
Ok(Some(include_dir))
@@ -69,8 +83,14 @@ fn wolfssl_include_dir() -> Result<Option<String>> {
6983
/// If `WOLFSSL_PREFIX` is set, returns `{WOLFSSL_PREFIX}/lib`.
7084
/// Otherwise falls back to the in-tree build output directory if it exists.
7185
fn wolfssl_lib_dir() -> Result<Option<String>> {
72-
if let Ok(prefix) = env::var("WOLFSSL_PREFIX") {
73-
Ok(Some(format!("{}/lib", prefix)))
86+
if let Some(prefix) = wolfssl_user_prefix() {
87+
let lib_dir = format!("{}/lib", prefix);
88+
let lib_path = Path::new(&lib_dir);
89+
if !lib_path.is_dir() {
90+
println!("cargo:warning=WOLFSSL_PREFIX is set but {} is not a directory", lib_dir);
91+
return Ok(None);
92+
}
93+
Ok(Some(lib_dir))
7494
} else {
7595
let repo_lib_dir = wolfssl_repo_lib_dir()?;
7696
if Path::new(&repo_lib_dir).exists() {

0 commit comments

Comments
 (0)