Skip to content

Commit 421f06b

Browse files
authored
Fail fast and more helpfully on invalid compile argument (#655)
1 parent a5bda9c commit 421f06b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/lib.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use std::ffi::{OsStr, OsString};
6262
use std::fmt::{self, Display};
6363
use std::fs;
6464
use std::io::{self, BufRead, BufReader, Read, Write};
65-
use std::path::{Path, PathBuf};
65+
use std::path::{Component, Path, PathBuf};
6666
use std::process::{Child, Command, Stdio};
6767
use std::sync::{Arc, Mutex};
6868
use std::thread::{self, JoinHandle};
@@ -139,6 +139,8 @@ enum ErrorKind {
139139
ToolExecError,
140140
/// Error occurred due to missing external tools.
141141
ToolNotFound,
142+
/// One of the function arguments failed validation.
143+
InvalidArgument,
142144
}
143145

144146
/// Represents an internal error that occurred, with an explanation.
@@ -943,6 +945,17 @@ impl Build {
943945
///
944946
/// This will return a result instead of panicing; see compile() for the complete description.
945947
pub fn try_compile(&self, output: &str) -> Result<(), Error> {
948+
let mut output_components = Path::new(output).components();
949+
match (output_components.next(), output_components.next()) {
950+
(Some(Component::Normal(_)), None) => {}
951+
_ => {
952+
return Err(Error::new(
953+
ErrorKind::InvalidArgument,
954+
"argument of `compile` must be a single normal path component",
955+
));
956+
}
957+
}
958+
946959
let (lib_name, gnu_lib_name) = if output.starts_with("lib") && output.ends_with(".a") {
947960
(&output[3..output.len() - 2], output.to_owned())
948961
} else {

0 commit comments

Comments
 (0)