Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite input file if it has no extension #34005

Closed
wants to merge 1 commit into from

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jun 1, 2016

Check wether the source file has the same path as the executable to be
generated and fail early if it would overwrite the source file with the
executable.

% echo 'fn main(){}' > file && ./rustc file
error: the input file would be overwritten by the generated executable
error: aborting due to previous error

Fixes #13019

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Aatch (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

// the source file.
let output_filenames = driver::build_output_filenames(&input, &odir, &ofile, &[], &sess);
match input_file_path {
Some(ifile) => if output_filenames.path(OutputType::Exe) == ifile {
Copy link
Contributor

@LeoTestard LeoTestard Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... Why checking only the executable output type? rustc can generate bytecode or assembly too and, as far as I know, all could overwrite the input file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


pub fn is_path_used(&self, path: PathBuf) -> bool {
for flavor in self.outputs.keys() {
if self.path(*flavor) == path {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a .iter() method that directly returns an iterator over (key, value) pairs. In fact, in this context, I think you do not even care about keys and could even use .values() to iterate over values only instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt that doing it this way I was hedging against path ever changing behaviour. Having said that, I just pushed a change that doesn't use path for this check.

@estebank
Copy link
Contributor Author

estebank commented Jun 3, 2016

r? @LeoTestard

@LeoTestard
Copy link
Contributor

@estebank I don't have the r+ rights yet, I usually ask someone (typically @pnkfelix) to r+ once I reviewed the changes. :P But looks good to me! Maybe you might want to trigger a new Travis build? (This one failed for some reason...)

@estebank
Copy link
Contributor Author

estebank commented Jun 3, 2016

@estebank I don't have the r+ rights yet, I usually ask someone (typically @pnkfelix) to r+ once I reviewed the changes. :P

r? @pnkfelix

But looks good to me!

Thanks, @LeoTestard!

Maybe you might want to trigger a new Travis build? (This one failed for some reason...)

I've been seeing travis failures when setting up the state, I have no idea why that is, but all PRs seemed to be affected for the last couple of days. Just rebased -i and pushed to trigger the build.

@rust-highfive rust-highfive assigned pnkfelix and unassigned Aatch Jun 3, 2016
@estebank estebank force-pushed the sourcefilecheck branch 3 times, most recently from e2ef035 to cc6cd1f Compare June 7, 2016 01:20
@jonas-schievink
Copy link
Contributor

@estebank Travis fail is legit

@pnkfelix
Copy link
Member

In particular here is the error:

failures:

---- [run-pass] run-pass/compiler-calls.rs stdout ----

error: test run failed!
status: exit code: 101
command: x86_64-unknown-linux-gnu/test/run-pass-fulldeps/compiler-calls.stage2-x86_64-unknown-linux-gnu 
stdout:
------------------------------------------

------------------------------------------
stderr:
------------------------------------------
error: the input file would be overwritten by the generated executable
thread 'main' panicked at 'assertion failed: `(left == right)` (left: `10`, right: `30`)', /build/src/test/run-pass-fulldeps/compiler-calls.rs:86
note: Run with `RUST_BACKTRACE=1` for a backtrace.

------------------------------------------

thread '[run-pass] run-pass/compiler-calls.rs' panicked at 'explicit panic', /build/src/tools/compiletest/src/runtest.rs:2243
note: Run with `RUST_BACKTRACE=1` for a backtrace.


failures:
    [run-pass] run-pass/compiler-calls.rs

test result: FAILED. 58 passed; 1 failed; 0 ignored; 0 measured

I have not attempted to dissect the problem completely; I thought the problem might be that its interpreted the test as overwriting compiler-calls, but the first argument is thrown away by the option parser, so now I don't know what the actual problem is.


@estebank in any case, you may want to revise your error message to actual include the input file name your code is predicting will be overwritten. That will make it easier for users trying to understand your message to connect it up to the potentially erroneous input they provided.

@bors
Copy link
Contributor

bors commented Aug 1, 2016

☔ The latest upstream changes (presumably #34830) made this pull request unmergeable. Please resolve the merge conflicts.

@nagisa
Copy link
Member

nagisa commented Aug 23, 2016

Breaking change for anybody scripting such behaviour.

Check wether the source file has the same path as the executable to be
generated and fail early if it would overwrite the source file with the
executable.

```bash
% echo 'fn main(){}' > file && ./rustc file
error: the input file "file" would be overwritten by the generated executable
error: aborting due to previous error

% echo 'fn main(){}' > file.rs && ./rustc file.rs -o file.rs
error: the input file "file.rs" would be overwritten by the generated executable
error: aborting due to previous error
```
@pnkfelix
Copy link
Member

I'm closing due to inactivity, but feel free to open a new pull request if you rebase and fix the aforementioned problems.

@pnkfelix pnkfelix closed this Sep 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants