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

rustc: Prevent false positives in crate loading #13017

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions src/librustc/metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,38 +284,46 @@ impl<'a> Context<'a> {
// reading dylib metadata is quite slow.
fn extract_one(&mut self, m: HashSet<Path>, flavor: &str,
slot: &mut Option<MetadataBlob>) -> Option<Path> {
if m.len() == 0 { return None }
if m.len() > 1 {
self.sess.span_err(self.span,
format!("multiple {} candidates for `{}` \
found", flavor, self.crate_id.name));
for (i, path) in m.iter().enumerate() {
self.sess.span_note(self.span,
format!(r"candidate \#{}: {}", i + 1,
path.display()));
}
return None
}
let mut ret = None::<Path>;
let mut error = 0;

let lib = m.move_iter().next().unwrap();
if slot.is_none() {
for lib in m.move_iter() {
info!("{} reading metadata from: {}", flavor, lib.display());
match get_metadata_section(self.os, &lib) {
let metadata = match get_metadata_section(self.os, &lib) {
Ok(blob) => {
if self.crate_matches(blob.as_slice()) {
*slot = Some(blob);
blob
} else {
info!("metadata mismatch");
return None;
continue
}
}
Err(_) => {
info!("no metadata found");
return None
continue
}
};
if ret.is_some() {
self.sess.span_err(self.span,
format!("multiple {} candidates for `{}` \
found", flavor, self.crate_id.name));
self.sess.span_note(self.span,
format!(r"candidate \#1: {}",
ret.get_ref().display()));
error = 1;
ret = None;
}
if error > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this cause the note to be printed twice for candidate 1?

Copy link
Member Author

Choose a reason for hiding this comment

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

A little subtle, but the first errors prints the previously found library (stored in ret), and the second prints prints the current library (stored in lib)

error += 1;
self.sess.span_note(self.span,
format!(r"candidate \#{}: {}", error,
lib.display()));
continue
}
*slot = Some(metadata);
ret = Some(lib);
}
return Some(lib);
return if error > 0 {None} else {ret}
}

fn crate_matches(&mut self, crate_data: &[u8]) -> bool {
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/suspicious-library/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-include ../tools.mk

all:
$(RUSTC) foo.rs
touch $(call DYLIB,foo-something-special)
touch $(call DYLIB,foo-something-special2)
$(RUSTC) bar.rs
13 changes: 13 additions & 0 deletions src/test/run-make/suspicious-library/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2014 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.

extern crate foo;

fn main() { foo::foo() }
13 changes: 13 additions & 0 deletions src/test/run-make/suspicious-library/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2014 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.

#[crate_type = "dylib"];

pub fn foo() {}