Skip to content

Commit eb2d992

Browse files
Fix handling of circular symbolic link chains
1 parent 66c1889 commit eb2d992

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

tree/readlink.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
use clap::Parser;
1111
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
12-
use std::collections::HashSet;
1312
use std::error::Error;
14-
use std::ffi::OsString;
1513
use std::fs;
1614
use std::io::Write;
1715
use std::io::{stderr, stdout, ErrorKind};
@@ -204,31 +202,34 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
204202
fn recursive_resolve(starting_path_buf: PathBuf) -> Result<PathBuf, String> {
205203
let mut current_path_buf = starting_path_buf;
206204

207-
let mut encountered_paths = HashSet::<OsString>::new();
205+
let mut recursion_level = 0_usize;
208206

209207
#[allow(clippy::while_let_loop)]
210208
loop {
211209
match fs::read_link(current_path_buf.as_path()) {
212210
Ok(pa) => {
211+
recursion_level += 1_usize;
212+
213+
// https://unix.stackexchange.com/questions/53087/how-do-you-increase-maxsymlinks
214+
if recursion_level == 40_usize {
215+
return Err(format!(
216+
"Symbolic link chain is circular or just too long, gave up at \"{}\"",
217+
current_path_buf.to_string_lossy()
218+
));
219+
}
220+
213221
if pa.is_absolute() {
214222
current_path_buf = pa;
215223
} else {
216224
if !current_path_buf.pop() {
217225
return Err(format!(
218-
"Could not remove last path segment from path \"{}\")",
226+
"Could not remove last path segment from path \"{}\"",
219227
current_path_buf.to_string_lossy()
220228
));
221229
}
222230

223231
current_path_buf.push(pa);
224232
}
225-
226-
if !encountered_paths.insert(current_path_buf.as_os_str().to_owned()) {
227-
return Err(format!(
228-
"Infinite symbolic link loop detected at \"{}\")",
229-
current_path_buf.to_string_lossy()
230-
));
231-
}
232233
}
233234
Err(_) => {
234235
break;

0 commit comments

Comments
 (0)