Prevent rustc overwriting input files
If rustc is invoked on a file that would be overwritten by the compilation, the compilation now fails, to avoid accidental loss. This resolves #13019.
This commit is contained in:
parent
53a6d14e5b
commit
c76cdce3d9
5 changed files with 63 additions and 3 deletions
|
@ -528,6 +528,25 @@ impl OutputFilenames {
|
|||
pub fn filestem(&self) -> String {
|
||||
format!("{}{}", self.out_filestem, self.extra)
|
||||
}
|
||||
|
||||
pub fn contains_path(&self, input_path: &PathBuf) -> bool {
|
||||
let input_path = input_path.canonicalize().ok();
|
||||
if input_path.is_none() {
|
||||
return false
|
||||
}
|
||||
match self.single_output_file {
|
||||
Some(ref output_path) => output_path.canonicalize().ok() == input_path,
|
||||
None => {
|
||||
for k in self.outputs.keys() {
|
||||
let output_path = self.path(k.to_owned());
|
||||
if output_path.canonicalize().ok() == input_path {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn host_triple() -> &'static str {
|
||||
|
@ -596,6 +615,12 @@ impl Options {
|
|||
).map(|(src, dst)| (src.clone(), dst.clone())).collect()
|
||||
)
|
||||
}
|
||||
|
||||
/// True if there will be an output file generated
|
||||
pub fn will_create_output_file(&self) -> bool {
|
||||
!self.debugging_opts.parse_only && // The file is just being parsed
|
||||
!self.debugging_opts.ls // The file is just being queried
|
||||
}
|
||||
}
|
||||
|
||||
// The type of entry function, so
|
||||
|
|
|
@ -232,11 +232,25 @@ pub fn run_compiler<'a>(args: &[String],
|
|||
let loader = file_loader.unwrap_or(box RealFileLoader);
|
||||
let codemap = Rc::new(CodeMap::with_file_loader(loader, sopts.file_path_mapping()));
|
||||
let mut sess = session::build_session_with_codemap(
|
||||
sopts, input_file_path, descriptions, codemap, emitter_dest,
|
||||
sopts, input_file_path.clone(), descriptions, codemap, emitter_dest,
|
||||
);
|
||||
rustc_trans::init(&sess);
|
||||
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
|
||||
|
||||
// Ensure the source file isn't accidentally overwritten during compilation.
|
||||
match input_file_path {
|
||||
Some(input_file_path) => {
|
||||
if driver::build_output_filenames(&input, &odir, &ofile, &[], &sess)
|
||||
.contains_path(&input_file_path) && sess.opts.will_create_output_file() {
|
||||
sess.err(&format!(
|
||||
"the input file \"{}\" would be overwritten by the generated executable",
|
||||
input_file_path.display()));
|
||||
return (Err(CompileIncomplete::Stopped), Some(sess));
|
||||
}
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
|
||||
let mut cfg = config::build_configuration(&sess, cfg);
|
||||
target_features::add_configuration(&mut cfg, &sess);
|
||||
sess.parse_sess.config = cfg;
|
||||
|
|
10
src/test/run-make/output-filename-overwrites-input/Makefile
Normal file
10
src/test/run-make/output-filename-overwrites-input/Makefile
Normal file
|
@ -0,0 +1,10 @@
|
|||
-include ../tools.mk
|
||||
|
||||
all:
|
||||
cp foo.rs $(TMPDIR)/foo
|
||||
$(RUSTC) $(TMPDIR)/foo 2>&1 \
|
||||
| $(CGREP) -e "the input file \".*foo\" would be overwritten by the generated executable"
|
||||
$(RUSTC) foo.rs 2>&1 && $(RUSTC) -Z ls $(TMPDIR)/foo 2>&1
|
||||
cp foo.rs $(TMPDIR)/foo.rs
|
||||
$(RUSTC) $(TMPDIR)/foo.rs -o $(TMPDIR)/foo.rs 2>&1 \
|
||||
| $(CGREP) -e "the input file \".*foo.rs\" would be overwritten by the generated executable"
|
11
src/test/run-make/output-filename-overwrites-input/foo.rs
Normal file
11
src/test/run-make/output-filename-overwrites-input/foo.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn main() {}
|
|
@ -7,8 +7,8 @@ all:
|
|||
cp foo.rs $(TMPDIR)/.foo.bar
|
||||
$(RUSTC) $(TMPDIR)/.foo.bar 2>&1 \
|
||||
| $(CGREP) -e "invalid character.*in crate name:"
|
||||
cp foo.rs $(TMPDIR)/+foo+bar
|
||||
$(RUSTC) $(TMPDIR)/+foo+bar 2>&1 \
|
||||
cp foo.rs $(TMPDIR)/+foo+bar.rs
|
||||
$(RUSTC) $(TMPDIR)/+foo+bar.rs 2>&1 \
|
||||
| $(CGREP) -e "invalid character.*in crate name:"
|
||||
cp foo.rs $(TMPDIR)/-foo.rs
|
||||
$(RUSTC) $(TMPDIR)/-foo.rs 2>&1 \
|
||||
|
|
Loading…
Reference in a new issue