Skip to content

Commit

Permalink
FileSystem: fix a bug that could cause strlen being called with NULL …
Browse files Browse the repository at this point in the history
…pointer in debug build (#91)
  • Loading branch information
VladimirUmek committed Feb 5, 2025
1 parent fb2e3c4 commit 18c980f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Components/FileSystem/FileSystem.scvd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>

<component_viewer schemaVersion="0.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="Component_Viewer.xsd">
<component name="File System" shortname="FS" version="8.0.1"/> <!-- name and version of the component -->
<component name="File System" shortname="FS" version="8.0.2"/> <!-- name and version of the component -->

<typedefs>
<!-- Flash Sector information (Driver_Flash.h line 58) -->
Expand Down
4 changes: 2 additions & 2 deletions Components/FileSystem/Include/rl_fs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::File System
* Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved.
* Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved.
*------------------------------------------------------------------------------
* Name: rl_fs.h
* Purpose: File System API
Expand All @@ -14,7 +14,7 @@

#define MW_FS_VERSION_MAJOR 8
#define MW_FS_VERSION_MINOR 0
#define MW_FS_VERSION_PATCH 1
#define MW_FS_VERSION_PATCH 2

// ==== Enumeration, structures, defines ====

Expand Down
23 changes: 20 additions & 3 deletions Components/FileSystem/Source/fs_common.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::File System
* Copyright (c) 2004-2023 Arm Limited (or its affiliates). All rights reserved.
* Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved.
*------------------------------------------------------------------------------
* Name: fs_common.c
* Purpose: Common file system functions
Expand Down Expand Up @@ -205,8 +205,8 @@ uint32_t fs_strmatch (const char s1[], const char s2[], uint32_t len1, uint32_t
}

if (len2 != 0U) {
n1 = strlen (s1);
n2 = strlen (s2);
n1 = fs_strlen (s1);
n2 = fs_strlen (s2);

if ((n1 >= len2) && (n2 >= len2)) {
n1 -= len2;
Expand All @@ -224,6 +224,23 @@ uint32_t fs_strmatch (const char s1[], const char s2[], uint32_t len1, uint32_t
return (match);
}

/**
Get length of a string
\param[in] sp string pointer
\return length of the string
*/
uint32_t fs_strlen (const char sp[]) {
uint32_t i;

i = 0U;
if (sp != NULL) {
while (sp[i] != '\0') {
i++;
}
}
return (i);
}

/**
Read U16 byte-aligned from LE byte order
Expand Down
3 changes: 2 additions & 1 deletion Components/FileSystem/Source/fs_common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::File System
* Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved.
* Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved.
*------------------------------------------------------------------------------
* Name: fs_common.h
* Purpose: Common file system function definitions
Expand All @@ -18,6 +18,7 @@ extern int32_t fs_drive_id (const char *path, const char **fn);
extern int32_t fs_strpos (const char *sp, const char ch);
extern uint32_t fs_strncasecmp (const char *s1, const char *s2, uint32_t n);
extern uint32_t fs_strmatch (const char *s1, const char *s2, uint32_t len1, uint32_t len2);
extern uint32_t fs_strlen (const char *sp);

extern uint16_t get_u16 (const uint8_t *p16);
extern void set_u16 (uint8_t *p16, uint16_t val);
Expand Down
14 changes: 7 additions & 7 deletions Components/FileSystem/Source/fs_efs.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::File System
* Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved.
* Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved.
*------------------------------------------------------------------------------
* Name: fs_efs.c
* Purpose: Embedded File System Implementation
Expand Down Expand Up @@ -452,7 +452,7 @@ static const char *file_name_validate (const char *path) {
}
}
/* Path is not valid */
EvrFsEFS_FileNameInvalid (p, strlen(p));
EvrFsEFS_FileNameInvalid (p, fs_strlen(p));
return (NULL);
}

Expand Down Expand Up @@ -546,7 +546,7 @@ static fsStatus file_create (fsEFS_Handle *fh, const char *fname) {
}

/* Determine amount of free space required to store file name */
fn_len = strlen (fname) + 1U;
fn_len = fs_strlen (fname) + 1U;

/* Align file name length */
fn_len = (fn_len + 3U) & ~3U;
Expand Down Expand Up @@ -1769,7 +1769,7 @@ __WEAK fsStatus efs_open (int32_t handle, const char *fn, int32_t openmode) {
fsStatus status;

EvrFsEFS_FileOpen (handle, fn, openmode);
EvrFsEFS_FileName (fn, strlen(fn));
EvrFsEFS_FileName (fn, fs_strlen(fn));

if ((handle < 0) || (handle >= fs_efs_fh_cnt)) {
/* Invalid parameter: handle number out of range */
Expand Down Expand Up @@ -2245,7 +2245,7 @@ __WEAK fsStatus efs_delete (const char *path, fsEFS_Volume *vol) {

/* Deleting file */
EvrFsEFS_FileDelete (vol->DrvLet, path);
EvrFsEFS_FileName (path, strlen(path));
EvrFsEFS_FileName (path, fs_strlen(path));

path = file_name_validate (path);
if (path == NULL) {
Expand Down Expand Up @@ -2378,8 +2378,8 @@ __WEAK fsStatus efs_rename (const char *fn, const char *newname, fsEFS_Volume *v

/* Renaming file fn to newname */
EvrFsEFS_FileRename (vol->DrvLet, fn, newname);
EvrFsEFS_FileName (fn, strlen(fn));
EvrFsEFS_FileName (newname, strlen(newname));
EvrFsEFS_FileName (fn, fs_strlen(fn));
EvrFsEFS_FileName (newname, fs_strlen(newname));

stat = efs_vol_chk (EFS_STATUS_MOUNT, vol);
if (stat != fsOK) {
Expand Down
40 changes: 20 additions & 20 deletions Components/FileSystem/Source/fs_fat.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::File System
* Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved.
* Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved.
*------------------------------------------------------------------------------
* Name: fs_fat.c
* Purpose: FAT File System Implementation
Expand Down Expand Up @@ -3315,8 +3315,8 @@ static fsStatus label_read (fsFAT_Volume *vol, char *buf) {
while (entry_pos_inc (&pos, 1, vol));

#ifdef FS_DEBUG
if (strlen(buf) != 0) {
EvrFsFAT_LabelString (buf, strlen(buf));
if (fs_strlen(buf) != 0) {
EvrFsFAT_LabelString (buf, fs_strlen(buf));
} else {
EvrFsFAT_LabelNotSet (vol->DrvLet);
}
Expand Down Expand Up @@ -4002,7 +4002,7 @@ static fsStatus path_create (PATH_INFO *pinfo, bool mkdir, fsFAT_Volume *vol) {
if (name_nt_gen (sn, num++) == false) {
return (fsError);
}
pinfo_nt.fn_len = (uint8_t)strlen (sn);
pinfo_nt.fn_len = (uint8_t)fs_strlen (sn);
stat = frec_find_elink (&pinfo_nt, vol);
if (stat == fsFileNotFound) {
stat = frec_find (&pinfo_nt, vol);
Expand Down Expand Up @@ -4380,8 +4380,8 @@ __WEAK fsStatus fat_format (fsFAT_Volume *vol, const char *opt) {
/* Formatting drive */
EvrFsFAT_FormatDrive (vol->DrvLet);
#ifdef FS_DEBUG
if (strlen(opt) != 0) {
EvrFsFAT_OptionsString (opt, strlen(opt));
if (fs_strlen(opt) != 0) {
EvrFsFAT_OptionsString (opt, fs_strlen(opt));
}
#endif

Expand Down Expand Up @@ -4772,7 +4772,7 @@ __WEAK fsStatus fat_open (int32_t handle, const char *path, int32_t openmode) {
uint16_t mode;

EvrFsFAT_FileOpen (handle, path, openmode);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (path, fs_strlen(path));

if ((handle < 0) || (handle >= fs_fat_fh_cnt)) {
/* Invalid parameter: handle number out of range */
Expand Down Expand Up @@ -5547,10 +5547,10 @@ __WEAK fsStatus fat_delete (const char *path, const char *options, fsFAT_Volume

/* Deleting file */
EvrFsFAT_FileDelete (vol->DrvLet, path);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (path, fs_strlen(path));
#ifdef FS_DEBUG
if (strlen(options) != 0) {
EvrFsFAT_OptionsString (options, strlen(options));
if (fs_strlen(options) != 0) {
EvrFsFAT_OptionsString (options, fs_strlen(options));
}
#endif

Expand Down Expand Up @@ -5831,8 +5831,8 @@ __WEAK fsStatus fat_rename (const char *path, const char *newname, fsFAT_Volume

/* Renaming file */
EvrFsFAT_FileRename (vol->DrvLet, path, newname);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (newname, strlen(newname));
EvrFsFAT_PathName (path, fs_strlen(path));
EvrFsFAT_PathName (newname, fs_strlen(newname));

if ((path == NULL) || (newname == NULL)) {
/* Invalid parameters */
Expand All @@ -5842,7 +5842,7 @@ __WEAK fsStatus fat_rename (const char *path, const char *newname, fsFAT_Volume
if (path_validate (path)) {
return (fsInvalidPath);
}
if (name_validate (newname, strlen(newname)) == 0U) {
if (name_validate (newname, fs_strlen(newname)) == 0U) {
return (fsInvalidPath);
}

Expand Down Expand Up @@ -5948,7 +5948,7 @@ __WEAK fsStatus fat_mkdir (const char *path, fsFAT_Volume *vol) {

/* Creating directory */
EvrFsFAT_DirCreate (vol->DrvLet, path);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (path, fs_strlen(path));

if (path == NULL) {
/* Invalid parameters */
Expand Down Expand Up @@ -6009,10 +6009,10 @@ __WEAK fsStatus fat_rmdir (const char *path, const char *options, fsFAT_Volume *

/* Removing directory */
EvrFsFAT_DirRemove (vol->DrvLet, path, options);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (path, fs_strlen(path));
#ifdef FS_DEBUG
if (strlen(options) != 0) {
EvrFsFAT_OptionsString (options, strlen(options));
if (fs_strlen(options) != 0) {
EvrFsFAT_OptionsString (options, fs_strlen(options));
}
#endif

Expand Down Expand Up @@ -6271,7 +6271,7 @@ __WEAK fsStatus fat_chdir (const char *path, fsFAT_Volume *vol) {

/* Setting current directory */
EvrFsFAT_ChDir (vol->DrvLet, path);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (path, fs_strlen(path));

if (path == NULL) {
/* Invalid parameters */
Expand Down Expand Up @@ -6417,7 +6417,7 @@ __WEAK fsStatus fat_pwd (char *path, uint32_t len, fsFAT_Volume *vol) {
}
while (clus != child);

sz = strlen(&path[1]);
sz = fs_strlen(&path[1]);

if ((offs - sz) < 2) {
/* Input buffer to small */
Expand Down Expand Up @@ -6475,7 +6475,7 @@ __WEAK fsStatus fat_attrib (fsFAT_Volume *vol, const char *path, uint32_t attrib

/* Setting file attributes */
EvrFsFAT_AttribSet (vol->DrvLet, path, attrib);
EvrFsFAT_PathName (path, strlen(path));
EvrFsFAT_PathName (path, fs_strlen(path));

if ((path == NULL) || (attrib == 0U)) {
/* Invalid parameters */
Expand Down
6 changes: 3 additions & 3 deletions Components/FileSystem/Source/fs_mapi.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*------------------------------------------------------------------------------
* MDK Middleware - Component ::File System
* Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved.
* Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved.
*------------------------------------------------------------------------------
* Name: fs_mapi.c
* Purpose: File Maintenance API Functions
Expand Down Expand Up @@ -330,7 +330,7 @@ fsStatus ffind (const char *pattern, fsFileInfo *info) {
len2 = 0U;
}
else {
len2 = strlen (p);
len2 = fs_strlen (p);
len2 -= (uint32_t)(len1 + 1);
}
}
Expand Down Expand Up @@ -367,7 +367,7 @@ fsStatus ffind (const char *pattern, fsFileInfo *info) {
}
else {
/* No wildcard, must exactly match (case insensitive) */
if (fs_strncasecmp (&info->name[0], &p[0], strlen(info->name)) == 0) {
if (fs_strncasecmp (&info->name[0], &p[0], fs_strlen(info->name)) == 0) {
/* Exact match */
RETURN (fsOK);
}
Expand Down
3 changes: 2 additions & 1 deletion Documentation/Doxygen/FileSystem/src/revision_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<th>Description</th>
</tr>
<tr>
<td>V8.0.1</td>
<td>V8.0.2</td>
<td>
- fixed bug that could cause strlen being called with NULL pointer in debug build
- fixed bug in memory card layer for SPI mode that prevented correct device size recognition
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Doxygen/General/src/revision_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<td>V8.0.1-dev</td>
<td>
- Network Component Version 8.1.0
- FileSystem Component Version 8.0.1
- FileSystem Component Version 8.0.2
</td>
</tr>
<tr>
Expand Down
5 changes: 3 additions & 2 deletions Keil.MDK-Middleware.pdsc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
Network Component Version 8.1.0
- added support for simultaneous outgoing connections via different network interfaces
- fixed handling of Ethernet drivers not supporting multicast address filtering
FileSystem Component Version 8.0.1
FileSystem Component Version 8.0.2
- fixed bug that could cause strlen being called with NULL pointer in debug build
- fixed bug in memory card layer for SPI mode that prevented correct device size recognition
</release>
<release version="7.17.0" date="2024-01-17">
Expand Down Expand Up @@ -1158,7 +1159,7 @@
</bundle>

<!-- File System (MDK) -->
<bundle Cbundle="MDK" Cclass="File System" Cversion="8.0.1">
<bundle Cbundle="MDK" Cclass="File System" Cversion="8.0.2">
<description>File Access on various storage devices</description>
<doc>Documentation/html/FileSystem/index.html</doc>
<component Cgroup="CORE" condition="CMSIS Core with RTOS and File System I/O">
Expand Down

0 comments on commit 18c980f

Please sign in to comment.