From 9453032bf69b9985955c4b563f1b41dcc659dc41 Mon Sep 17 00:00:00 2001 From: Sahan Fernando Date: Thu, 10 Dec 2020 06:56:50 +1100 Subject: [PATCH] Userland: Fix overly-eager loop detection in cp (#4368) The bug is that if you try to cp DIR_A to DIR_B where DIR_A and DIR_B have the same parent directory and DIR_A's name is a prefix of DIR_B (e.g. foo -> foo2, bar -> barbar), it thinks that it's a subdirectory (since it checks if DIR_A's realpath is a prefix of DIR_B's realpath). The easiest solution is to put a path delimiter at the end before the comparison, since you can't have a / in the middle of a directory name. For example if DIR_A is /home/anon/foo and DIR_B is /home/anon/foo2, then DIR_A's realpath is a prefix of DIR_B's realpath even though DIR_B is not inside DIR_A. --- Userland/cp.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Userland/cp.cpp b/Userland/cp.cpp index 915ec3e13b..ea1a099034 100644 --- a/Userland/cp.cpp +++ b/Userland/cp.cpp @@ -181,7 +181,9 @@ bool copy_directory(String src_path, String dst_path) } String src_rp = Core::File::real_path_for(src_path); + src_rp = String::format("%s/", src_rp.characters()); String dst_rp = Core::File::real_path_for(dst_path); + dst_rp = String::format("%s/", dst_rp.characters()); if (!dst_rp.is_empty() && dst_rp.starts_with(src_rp)) { fprintf(stderr, "cp: Cannot copy %s into itself (%s)\n",