Skip to content

Commit 79d6205

Browse files
CopilotBernardXiong
andcommitted
[libc][libdl] Extract name extraction to shared function and fix edge cases
Address code review feedback: 1. Extract path stripping logic into shared dlmodule_extract_name() function to avoid code duplication between dlopen() and _dlmodule_set_name(). 2. Fix edge case handling for filenames starting with dot (e.g., .hidden). 3. Add proper documentation for the new function. This ensures consistent module name extraction across all code paths. Co-authored-by: BernardXiong <[email protected]>
1 parent 8a99de1 commit 79d6205

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

components/libc/posix/libdl/dlmodule.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,28 @@ static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
3333
#pragma section="RTMSymTab"
3434
#endif
3535

36-
/* set the name of module */
37-
static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
36+
/**
37+
* @brief Extract module name from a file path by stripping directory and extension.
38+
*
39+
* @param path the file path (e.g., "/mnt/sdcard/apps/clock.so")
40+
* @param name buffer to store the extracted module name
41+
* @param name_size size of the name buffer
42+
*
43+
* @note This function extracts the base name without path and extension.
44+
* For example: "/mnt/sdcard/apps/clock.so" -> "clock"
45+
* For hidden files like ".hidden", the entire filename is used.
46+
*/
47+
void dlmodule_extract_name(const char *path, char *name, int name_size)
3848
{
3949
int size;
40-
struct rt_object *object;
41-
const char *first, *end, *ptr;
50+
const char *first, *end, *ptr, *last_dot;
4251

43-
object = &(module->parent);
44-
ptr = first = (char *)path;
45-
end = path + rt_strlen(path);
52+
RT_ASSERT(path != RT_NULL);
53+
RT_ASSERT(name != RT_NULL);
54+
RT_ASSERT(name_size > 0);
55+
56+
ptr = first = path;
57+
end = path + rt_strlen(path);
4658

4759
/* find the start of filename (after last '/') */
4860
while (*ptr != '\0')
@@ -52,25 +64,44 @@ static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
5264
ptr++;
5365
}
5466

55-
/* find extension in filename portion only (after last '/') */
67+
/* find last extension in filename portion only (after last '/') */
68+
last_dot = RT_NULL;
5669
ptr = first;
5770
while (*ptr != '\0')
5871
{
5972
if (*ptr == '.')
60-
end = ptr;
73+
last_dot = ptr;
6174
ptr++;
6275
}
6376

77+
/* determine end position for module name */
78+
if (last_dot != RT_NULL && last_dot != first)
79+
{
80+
/* extension found and filename doesn't start with dot */
81+
end = last_dot;
82+
}
83+
/* else: no extension or filename starts with dot, use entire filename */
84+
6485
size = end - first;
6586
if (size <= 0)
6687
{
67-
/* no extension found, use entire filename */
88+
/* edge case: empty filename or only dot(s) */
6889
size = rt_strlen(first);
6990
}
70-
if (size >= RT_NAME_MAX) size = RT_NAME_MAX - 1;
91+
if (size >= name_size)
92+
size = name_size - 1;
93+
94+
rt_strncpy(name, first, size);
95+
name[size] = '\0';
96+
}
97+
98+
/* set the name of module */
99+
static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
100+
{
101+
struct rt_object *object;
71102

72-
rt_strncpy(object->name, first, size);
73-
object->name[size] = '\0';
103+
object = &(module->parent);
104+
dlmodule_extract_name(path, object->name, RT_NAME_MAX);
74105
}
75106

76107
#define RT_MODULE_ARG_MAX 8

components/libc/posix/libdl/dlmodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void dlmodule_exit(int ret_code);
8282

8383
struct rt_dlmodule *dlmodule_find(const char *name);
8484

85+
void dlmodule_extract_name(const char *path, char *name, int name_size);
86+
8587
rt_ubase_t dlmodule_symbol_find(const char *sym_str);
8688

8789
#endif

components/libc/posix/libdl/dlopen.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +56,7 @@ void* dlopen(const char *filename, int flags)
5656
* This matches the logic in _dlmodule_set_name() so that dlmodule_find()
5757
* can properly locate already-loaded modules by their stored name.
5858
*/
59-
{
60-
const char *first, *end, *ptr;
61-
int size;
62-
63-
ptr = first = fullpath;
64-
end = fullpath + strlen(fullpath);
65-
66-
while (*ptr != '\0')
67-
{
68-
if (*ptr == '/')
69-
first = ptr + 1;
70-
ptr++;
71-
}
72-
73-
/* find extension in filename portion only (after last '/') */
74-
ptr = first;
75-
while (*ptr != '\0')
76-
{
77-
if (*ptr == '.')
78-
end = ptr;
79-
ptr++;
80-
}
81-
82-
size = end - first;
83-
if (size <= 0)
84-
{
85-
/* no extension found, use entire filename */
86-
size = strlen(first);
87-
}
88-
if (size >= RT_NAME_MAX)
89-
size = RT_NAME_MAX - 1;
90-
91-
rt_strncpy(module_name, first, size);
92-
module_name[size] = '\0';
93-
}
59+
dlmodule_extract_name(fullpath, module_name, RT_NAME_MAX);
9460

9561
rt_enter_critical();
9662

0 commit comments

Comments
 (0)