Skip to content

Commit d82dd71

Browse files
authored
[bugfix][component/dfs] fix bugs for function _get_parent_path(). (#10539)
* [bugfix][component/dfs]1.Skip the trailing slash character failed. 2. Scenario that parent path is root is not considered. * replace strdup() by rt_strdup(). * free memory after strdup(). * fix issue of not appending '\0' at end when parent path is root.
1 parent c55cbc5 commit d82dd71

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

components/dfs/dfs_v2/src/dfs_file.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -50,24 +50,39 @@ static int _get_parent_path(const char *fullpath, char *path)
5050
int len = 0;
5151
char *str = 0;
5252

53-
str = strrchr(fullpath, '/');
53+
char *full_path = rt_strdup(fullpath);
54+
if (full_path == NULL)
55+
{
56+
rt_set_errno(ENOMEM);
57+
return -1;
58+
}
59+
60+
str = strrchr(full_path, '/');
5461

5562
/* skip last '/' */
5663
if (str && *(str + 1) == '\0')
5764
{
58-
str = strrchr(str - 1, '/');
65+
*str = '\0';
66+
str = strrchr(full_path, '/');
5967
}
6068

6169
if (str)
6270
{
63-
len = str - fullpath;
71+
len = str - full_path;
6472
if (len > 0)
6573
{
66-
rt_memcpy(path, fullpath, len);
74+
rt_memcpy(path, full_path, len);
6775
path[len] = '\0';
6876
}
77+
else if (len == 0) /* parent path is root path. */
78+
{
79+
path[0] = '/';
80+
path[1] = '\0';
81+
len = 1;
82+
}
6983
}
7084

85+
rt_free(full_path);
7186
return len;
7287
}
7388

@@ -260,7 +275,7 @@ char *dfs_file_realpath(struct dfs_mnt **mnt, const char *fullpath, int mode)
260275
{
261276
int len, link_len;
262277

263-
path = (char *)rt_malloc((DFS_PATH_MAX * 3) + 3); // path + \0 + link_fn + \0 + tmp_path + \0
278+
path = (char *)rt_malloc((DFS_PATH_MAX * 3) + 3); /* path + \0 + link_fn + \0 + tmp_path + \0 */
264279
if (!path)
265280
{
266281
return RT_NULL;
@@ -2356,14 +2371,14 @@ void copy(const char *src, const char *dst)
23562371
flag |= FLAG_DST_IS_FILE;
23572372
}
23582373

2359-
//2. check status
2374+
/* 2. check status */
23602375
if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
23612376
{
23622377
rt_kprintf("cp faild, cp dir to file is not permitted!\n");
23632378
return ;
23642379
}
23652380

2366-
//3. do copy
2381+
/* 3. do copy */
23672382
if (flag & FLAG_SRC_IS_FILE)
23682383
{
23692384
if (flag & FLAG_DST_IS_DIR)
@@ -2383,7 +2398,7 @@ void copy(const char *src, const char *dst)
23832398
copyfile(src, dst);
23842399
}
23852400
}
2386-
else //flag & FLAG_SRC_IS_DIR
2401+
else /* flag & FLAG_SRC_IS_DIR */
23872402
{
23882403
if (flag & FLAG_DST_IS_DIR)
23892404
{
@@ -2411,4 +2426,4 @@ void copy(const char *src, const char *dst)
24112426
}
24122427
FINSH_FUNCTION_EXPORT(copy, copy file or dir)
24132428

2414-
#endif
2429+
#endif

0 commit comments

Comments
 (0)