Skip to content

Commit ae16d3e

Browse files
committed
Fix NULL pointer indirection in FilePath
If the FileBase::lookup operation in the constructor of FilePath returns NULL, subsequent operations (such as isFile()/isFileSystem()) will call methods on a NULL 'fb' pointer. This commit fixes this issue by adding explicit NULL checks and a new method in FilePath (exists()).
1 parent 96101ae commit ae16d3e

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

libraries/mbed/api/FilePath.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FilePath {
3434

3535
bool isFile(void);
3636
FileLike* file(void);
37+
bool exists(void);
3738

3839
private:
3940
const char* file_name;

libraries/mbed/common/FilePath.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ FilePath::FilePath(const char* file_path) : file_name(NULL), fb(NULL) {
3636
file_name++;
3737
}
3838

39-
FileBase::lookup(file_system, len);
40-
41-
4239
fb = FileBase::lookup(file_system, len);
4340
}
4441

@@ -47,6 +44,8 @@ const char* FilePath::fileName(void) {
4744
}
4845

4946
bool FilePath::isFileSystem(void) {
47+
if (NULL == fb)
48+
return false;
5049
return (fb->getPathType() == FileSystemPathType);
5150
}
5251

@@ -58,6 +57,8 @@ FileSystemLike* FilePath::fileSystem(void) {
5857
}
5958

6059
bool FilePath::isFile(void) {
60+
if (NULL == fb)
61+
return false;
6162
return (fb->getPathType() == FilePathType);
6263
}
6364

@@ -68,4 +69,8 @@ FileLike* FilePath::file(void) {
6869
return NULL;
6970
}
7071

72+
bool FilePath::exists(void) {
73+
return fb != NULL;
74+
}
75+
7176
} // namespace mbed

libraries/mbed/common/retarget.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
163163
} else {
164164
FilePath path(name);
165165

166-
if (path.isFile()) {
166+
if (!path.exists())
167+
return -1;
168+
else if (path.isFile()) {
167169
res = path.file();
168170
} else {
169171
FileSystemLike *fs = path.fileSystem();

0 commit comments

Comments
 (0)