From ae16d3efa83d43db583f04bdf7f98be4e3d763a4 Mon Sep 17 00:00:00 2001 From: Bogdan Marinescu Date: Thu, 5 Sep 2013 14:09:27 +0300 Subject: [PATCH] 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()). --- libraries/mbed/api/FilePath.h | 1 + libraries/mbed/common/FilePath.cpp | 11 ++++++++--- libraries/mbed/common/retarget.cpp | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/mbed/api/FilePath.h b/libraries/mbed/api/FilePath.h index 2e335bfeb6..3de1205048 100644 --- a/libraries/mbed/api/FilePath.h +++ b/libraries/mbed/api/FilePath.h @@ -34,6 +34,7 @@ public: bool isFile(void); FileLike* file(void); + bool exists(void); private: const char* file_name; diff --git a/libraries/mbed/common/FilePath.cpp b/libraries/mbed/common/FilePath.cpp index 7dbf710656..09147a2699 100644 --- a/libraries/mbed/common/FilePath.cpp +++ b/libraries/mbed/common/FilePath.cpp @@ -36,9 +36,6 @@ FilePath::FilePath(const char* file_path) : file_name(NULL), fb(NULL) { file_name++; } - FileBase::lookup(file_system, len); - - fb = FileBase::lookup(file_system, len); } @@ -47,6 +44,8 @@ const char* FilePath::fileName(void) { } bool FilePath::isFileSystem(void) { + if (NULL == fb) + return false; return (fb->getPathType() == FileSystemPathType); } @@ -58,6 +57,8 @@ FileSystemLike* FilePath::fileSystem(void) { } bool FilePath::isFile(void) { + if (NULL == fb) + return false; return (fb->getPathType() == FilePathType); } @@ -68,4 +69,8 @@ FileLike* FilePath::file(void) { return NULL; } +bool FilePath::exists(void) { + return fb != NULL; +} + } // namespace mbed diff --git a/libraries/mbed/common/retarget.cpp b/libraries/mbed/common/retarget.cpp index 987e5e4242..90a5dbe0e9 100644 --- a/libraries/mbed/common/retarget.cpp +++ b/libraries/mbed/common/retarget.cpp @@ -163,7 +163,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { } else { FilePath path(name); - if (path.isFile()) { + if (!path.exists()) + return -1; + else if (path.isFile()) { res = path.file(); } else { FileSystemLike *fs = path.fileSystem();