Skip to content

Commit

Permalink
switch from filter_map() to map_while()
Browse files Browse the repository at this point in the history
It's possible for a call to `File::open()` to succeed even if reading from
that `File` isn't actually possible due to the `File` being a directory.
In such cases, `Lines` can return `Err` infinitely,
causing `filter_map()` to diverge while searching for a nonexistant `Ok`.

This is a semantic change.
`filter_map()` can filter multiple errors before returning lines again,
while `map_while()` will stop iterating entirely at the first `Err`.

However, errors are unlikely at all since the `file` argument is currently
always a preallocated temporary file for collecting `Command` output.
This merely mitigates the worst case if `lines_from_file()` is ever used
in a way where this error could occur.
  • Loading branch information
abysssol committed Jul 30, 2024
1 parent da632b4 commit 9925e49
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion ofborg/src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl Nix {
fn lines_from_file(file: fs::File) -> Vec<String> {
BufReader::new(file)
.lines()
.filter_map(|line| line.ok())
.map_while(Result::ok)
.filter(|msg| !is_user_setting_warning(msg))
.collect()
}
Expand Down

0 comments on commit 9925e49

Please sign in to comment.