Skip to content

Commit

Permalink
Check for errors from str_path_to_cfstring_ref.
Browse files Browse the repository at this point in the history
Closes #301.
  • Loading branch information
jorendorff authored and 0xpr03 committed May 4, 2021
1 parent 4a3f5a3 commit 86421e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ impl FsEventWatcher {
unsafe {
let mut err: cf::CFErrorRef = ptr::null_mut();
let cf_path = cf::str_path_to_cfstring_ref(str_path, &mut err);
if cf_path.is_null() {
cf::CFRelease(err as cf::CFRef);
return Err(Error::watch_not_found().add_path(path.as_ref().into()));
}

let mut to_remove = Vec::new();
for idx in 0..cf::CFArrayGetCount(self.paths) {
Expand Down Expand Up @@ -320,6 +324,12 @@ impl FsEventWatcher {
unsafe {
let mut err: cf::CFErrorRef = ptr::null_mut();
let cf_path = cf::str_path_to_cfstring_ref(str_path, &mut err);
if cf_path.is_null() {
// Most likely the directory was deleted, or permissions changed,
// while the above code was running.
cf::CFRelease(err as cf::CFRef);
return Err(Error::path_not_found().add_path(path.as_ref().into()));
}
cf::CFArrayAppendValue(self.paths, cf_path);
cf::CFRelease(cf_path);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/race-with-remove-dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{env, fs, thread, time::Duration};

use notify::{immediate_watcher, RecursiveMode, Watcher};

/// Test for <https://github.com/notify-rs/notify/issues/301>.
/// Note: This test will fail if your temp directory is not writable.
#[test]
fn test_race_with_remove_dir() {
let tmpdir = env::temp_dir().join(".tmprPcUcB");
fs::create_dir_all(&tmpdir).unwrap();

{
let tmpdir = tmpdir.clone();
thread::spawn(move || {
let mut watcher = immediate_watcher(move |result| {
eprintln!("received event: {:?}", result);
})
.unwrap();

watcher.watch(tmpdir, RecursiveMode::NonRecursive).unwrap();
});
}

let subdir = tmpdir.join("146d921d.tmp");
fs::create_dir_all(&subdir).unwrap();
fs::remove_dir_all(&tmpdir).unwrap();
thread::sleep(Duration::from_secs(1));
}

0 comments on commit 86421e5

Please sign in to comment.