Skip to content

Commit 9540aed

Browse files
authored
Merge pull request #6883 from makermelissa/main
Prevent folder from trying to move inside itself
2 parents e3688b4 + 6ee45dd commit 9540aed

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

extmod/vfs_fat.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,7 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_
225225
const char *old_path = mp_obj_str_get_str(path_in);
226226
const char *new_path = mp_obj_str_get_str(path_out);
227227

228-
// Check to see if we're moving a directory into itself. This occurs when we're moving a
229-
// directory where the old path is a prefix of the new and the next character is a "/" and thus
230-
// preserves the original directory name.
231-
FILINFO fno;
232-
FRESULT res = f_stat(&self->fatfs, old_path, &fno);
233-
if (res != FR_OK) {
234-
mp_raise_OSError_fresult(res);
235-
}
236-
if ((fno.fattrib & AM_DIR) != 0 &&
237-
strlen(new_path) > strlen(old_path) &&
238-
new_path[strlen(old_path)] == '/' &&
239-
strncmp(old_path, new_path, strlen(old_path)) == 0) {
240-
mp_raise_OSError(MP_EINVAL);
241-
}
242-
243-
res = f_rename(&self->fatfs, old_path, new_path);
228+
FRESULT res = f_rename(&self->fatfs, old_path, new_path);
244229
if (res == FR_EXIST) {
245230
// if new_path exists then try removing it (but only if it's a file)
246231
fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute

lib/oofatfs/ff.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4816,6 +4816,21 @@ FRESULT f_rename (
48164816
DEF_NAMBUF
48174817

48184818

4819+
// Check to see if we're moving a directory into itself. This occurs when we're moving a
4820+
// directory where the old path is a prefix of the new and the next character is a "/" and thus
4821+
// preserves the original directory name.
4822+
FILINFO fno;
4823+
res = f_stat(fs, path_old, &fno);
4824+
if (res != FR_OK) {
4825+
return res;
4826+
}
4827+
if ((fno.fattrib & AM_DIR) != 0 &&
4828+
strlen(path_new) > strlen(path_old) &&
4829+
path_new[strlen(path_old)] == '/' &&
4830+
strncmp(path_old, path_new, strlen(path_old)) == 0) {
4831+
return FR_INVALID_NAME;
4832+
}
4833+
48194834
res = find_volume(fs, FA_WRITE); /* Get logical drive of the old object */
48204835
if (res == FR_OK) {
48214836
djo.obj.fs = fs;

tests/extmod/vfs_fat_fileio2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def ioctl(self, op, arg):
7070
except OSError as e:
7171
print(e.errno == uerrno.ENOENT)
7272

73+
try:
74+
vfs.rename("foo_dir", "foo_dir/inside_itself")
75+
except OSError as e:
76+
print(e.errno == uerrno.EINVAL)
77+
7378
# file in dir
7479
with open("foo_dir/file-in-dir.txt", "w+t") as f:
7580
f.write("data in file")

tests/extmod/vfs_fat_fileio2.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
True
22
True
33
True
4+
True
45
b'data in file'
56
True
67
[('sub_file.txt', 32768, 0, 11), ('file.txt', 32768, 0, 12)]

0 commit comments

Comments
 (0)