Skip to content

Commit

Permalink
color: Fix an infinite loop colorizing some invalid paths
Browse files Browse the repository at this point in the history
Previously, given

    $ touch file
    $ ln -s file/file notdir
    $ bfs notdir/file

bfs would loop forever when printing the error message, since it
expected stripping the trailing slash from "notdir/" to fix the ENOTDIR
error, but the broken symlink still gave the same error.

Fixes: b4c3201 ("color: Only highlight the trailing slash on ENOTDIR")
  • Loading branch information
tavianator committed Oct 14, 2024
1 parent 295ae76 commit b89f22c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,22 +998,25 @@ static ssize_t first_broken_offset(const char *path, const struct BFTW *ftwbuf,
goto out;
}

size_t len = dstrlen(at_path);
while (ret > 0) {
dstresize(&at_path, len);
if (xfaccessat(at_fd, at_path, F_OK) == 0) {
break;
}

size_t len = dstrlen(at_path);
while (ret && at_path[len - 1] == '/') {
--len, --ret;
}
if (errno != ENOTDIR) {
while (ret && at_path[len - 1] != '/') {
// Try without trailing slashes, to distinguish "notdir/" from "notdir"
if (at_path[len - 1] == '/') {
do {
--len, --ret;
}
} while (ret > 0 && at_path[len - 1] == '/');
continue;
}

dstresize(&at_path, len);
// Remove the last component and try again
do {
--len, --ret;
} while (ret > 0 && at_path[len - 1] != '/');
}

out_path:
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions tests/bfs/color_notdir_slash_error.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Regression test: infinite loop printing the error message for .../notdir/nowhere
! bfs_diff -color links/notdir/nowhere

0 comments on commit b89f22c

Please sign in to comment.