From 51b51a6eb1e5963c37425da653f2ac4afdf561a4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 20 Oct 2023 18:29:50 +0300 Subject: [PATCH] Fix recursion in rm --- coreutils/rm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/coreutils/rm.c b/coreutils/rm.c index 2100754..fb87234 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -7,7 +7,7 @@ #include int get_stat(const char *path, struct stat *stat_path) { - if (stat(path, stat_path)) { + if (lstat(path, stat_path)) { fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno)); return 1; } @@ -17,9 +17,10 @@ int get_stat(const char *path, struct stat *stat_path) { int rmtree(const char *path) { struct stat stat_path; - get_stat(path, &stat_path); + if (get_stat(path, &stat_path)) + return 1; - if (S_ISDIR(stat_path.st_mode) == 0) { + if (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) { if (unlink(path) < 0) { fprintf(stderr, "rm: %s: is not directory\n", path); return 1;