Skip to content

Commit

Permalink
Change dynamic_library::open_external to take ToCStr
Browse files Browse the repository at this point in the history
`std::unstable::dynamic_library::open_external` currently takes a
`Path`, but because `Paths` produce normalized strings, this can
change the semantics of lookups in a given environment. This patch
generalizes the function to take a `ToCStr`-bounded type, which
includes both `Path`s and `str`s.

Closes rust-lang#11650.
  • Loading branch information
aturon committed May 7, 2014
1 parent bf58bfe commit b7058a7
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/libstd/unstable/dynamic_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ impl Drop for DynamicLibrary {
}

impl DynamicLibrary {
// FIXME (#12938): Until DST lands, we cannot decompose &str into
// & and str, so we cannot usefully take ToCStr arguments by
// reference (without forcing an additional & around &str). So we
// are instead temporarily adding an instance for &Path, so that
// we can take ToCStr as owned. When DST lands, the &Path instance
// should be removed, and arguments bound by ToCStr should be
// passed by reference. (Here: in the `open` method.)

/// Lazily open a dynamic library. When passed None it gives a
/// handle to the calling process
pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
pub fn open<T: ToCStr>(filename: Option<T>)
-> Result<DynamicLibrary, ~str> {
unsafe {
let mut filename = filename;
let maybe_library = dl::check_for_errors_in(|| {
match filename {
match filename.take() {
Some(name) => dl::open_external(name),
None => dl::open_internal()
}
Expand Down Expand Up @@ -109,7 +119,8 @@ mod test {
fn test_loading_cosine() {
// The math library does not need to be loaded since it is already
// statically linked in
let libm = match DynamicLibrary::open(None) {
let none: Option<Path> = None; // appease the typechecker
let libm = match DynamicLibrary::open(none) {
Err(error) => fail!("Could not load self as module: {}", error),
Ok(libm) => libm
};
Expand Down Expand Up @@ -137,7 +148,7 @@ mod test {
fn test_errors_do_not_crash() {
// Open /dev/null as a library to get an error, and make sure
// that only causes an error, and not a crash.
let path = GenericPath::new("/dev/null");
let path = Path::new("/dev/null");
match DynamicLibrary::open(Some(&path)) {
Err(_) => {}
Ok(_) => fail!("Successfully opened the empty library.")
Expand All @@ -152,12 +163,11 @@ mod test {
pub mod dl {
use c_str::ToCStr;
use libc;
use path;
use ptr;
use str;
use result::*;

pub unsafe fn open_external(filename: &path::Path) -> *u8 {
pub unsafe fn open_external<T: ToCStr>(filename: T) -> *u8 {
filename.with_c_str(|raw_name| {
dlopen(raw_name, Lazy as libc::c_int) as *u8
})
Expand Down

0 comments on commit b7058a7

Please sign in to comment.