Skip to content

Commit 6c25575

Browse files
CopilotJohnAmadis
andcommitted
Address code review feedback: improve NULL handling and documentation
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent 7c68a90 commit 6c25575

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

src/dmdevfs.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,21 +1171,31 @@ static int read_driver_node_path( const driver_node_t* node, char* path_buffer,
11711171

11721172
/**
11731173
* @brief Compare two paths, ignoring trailing slashes
1174+
* @param path1 First path to compare
1175+
* @param path2 Second path to compare
11741176
* @return 0 if equal, non-zero if different
1177+
*
1178+
* Note: The root path "/" is treated specially and retains its slash.
1179+
* For example, "/" and "//" are considered equal, but "dir" and "dir/" are also equal.
11751180
*/
11761181
static int compare_paths_ignore_trailing_slash( const char* path1, const char* path2 )
11771182
{
1183+
// Handle NULL pointers - both NULL is equal, one NULL is different
1184+
if (path1 == NULL && path2 == NULL)
1185+
{
1186+
return 0;
1187+
}
11781188
if (path1 == NULL || path2 == NULL)
11791189
{
1180-
return (path1 == path2) ? 0 : 1;
1190+
return 1;
11811191
}
11821192

11831193
// Get lengths
11841194
size_t len1 = strlen(path1);
11851195
size_t len2 = strlen(path2);
11861196

11871197
// Remove trailing slashes from both paths for comparison
1188-
// Keep at least "/" if that's the entire path
1198+
// Keep at least "/" if that's the entire path (len > 1 ensures we keep root "/")
11891199
while (len1 > 1 && path1[len1 - 1] == '/')
11901200
{
11911201
len1--;
@@ -1207,6 +1217,18 @@ static int compare_paths_ignore_trailing_slash( const char* path1, const char* p
12071217

12081218
/**
12091219
* @brief Compare the path of a driver directory with a given path
1220+
*
1221+
* This function compares the parent directory of a driver node with a given path.
1222+
* It's used by opendir/readdir to find all driver nodes that belong to a specific directory.
1223+
*
1224+
* @param data Pointer to driver_node_t
1225+
* @param user_data Pointer to directory path string
1226+
* @return 0 if the node's parent matches the given path, non-zero otherwise
1227+
*
1228+
* Example: When listing directory "dmspiflash0", this function finds all nodes
1229+
* whose parent directory is "dmspiflash0" (e.g., nodes with path "dmspiflash0/1").
1230+
*
1231+
* Note: Trailing slashes are ignored in comparison, so "dmspiflash0" matches "dmspiflash0/".
12101232
*/
12111233
static int compare_driver_directory( const void* data, const void* user_data )
12121234
{
@@ -1224,6 +1246,8 @@ static int compare_driver_directory( const void* data, const void* user_data )
12241246
}
12251247

12261248
// Use helper function to compare paths, handling optional trailing slashes
1249+
// This ensures exact path matching (not prefix matching) which is critical
1250+
// to prevent "/" from incorrectly matching subdirectories like "dmspiflash0/"
12271251
return compare_paths_ignore_trailing_slash(path, parent_dir);
12281252
}
12291253

0 commit comments

Comments
 (0)