Skip to content

Commit

Permalink
Stop following symlinks when removing file in bracket_tmpdir.
Browse files Browse the repository at this point in the history
  • Loading branch information
gildor478 committed Jan 24, 2020
1 parent b4f807d commit 82bed2c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v2.2.2 - 2020-01-24

### Fixed
- Don't follow symlink in bracket_tmpdir removal code (Closes: #11).

## v2.2.1 - 2019-10-02

### Fixed
Expand Down
7 changes: 6 additions & 1 deletion src/lib/ounit2/advanced/oUnitBracket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ let bracket_tmpdir ?(prefix="ounit-") ?(suffix=".dir") test_ctxt =
Array.iter
(fun bn ->
let fn' = Filename.concat fn bn in
let is_dir = try Sys.is_directory fn' with _ -> false in
let is_dir =
try
let st = Unix.lstat fn' in
st.Unix.st_kind = Unix.S_DIR
with _ -> false
in
if is_dir then begin
rmdir fn';
safe_run Unix.rmdir fn';
Expand Down
20 changes: 20 additions & 0 deletions test/testOUnitBracket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ let tests =
"Temporary directory doesn't exist anymore."
(not (Sys.file_exists dn)));

"tmpdir_with_symlink" >::
(fun test_ctxt ->
let () = TestCommon.skip_if_notunix () in
let tmpdn = bracket_tmpdir test_ctxt in
let tmpdn2 = Filename.concat tmpdn "bar" in
let _ =
Unix.mkdir tmpdn2 0700;
assert_bool
"Directory outside of temporary directory exists."
(Sys.file_exists tmpdn2);
with_bracket_holder
test_ctxt bracket_tmpdir
(fun dn ->
let target = Filename.concat dn "symlink" in
Unix.symlink tmpdn target)
in
assert_bool
"Directory outside of temporary directory still exists."
(Sys.file_exists tmpdn2));

"chdir" >::
(fun test_ctxt ->
let tmpdn = bracket_tmpdir test_ctxt in
Expand Down

0 comments on commit 82bed2c

Please sign in to comment.