From d8fe044c5aee082354c87c7aba9f4f2091a83c55 Mon Sep 17 00:00:00 2001 From: Bill Greiman Date: Fri, 15 May 2020 09:43:12 -0700 Subject: [PATCH] Add null pointer checks --- library.properties | 2 +- src/FatLib/FatFile.cpp | 19 ++++++++++--------- src/FatLib/FatFile.h | 2 +- src/FatLib/FatFileLFN.cpp | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/library.properties b/library.properties index 6e5db5d7..724cacf8 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SdFat -version=1.1.2 +version=1.1.3 license=MIT author=Bill Greiman maintainer=Bill Greiman diff --git a/src/FatLib/FatFile.cpp b/src/FatLib/FatFile.cpp index c02c0855..a47788ff 100644 --- a/src/FatLib/FatFile.cpp +++ b/src/FatLib/FatFile.cpp @@ -262,7 +262,7 @@ bool FatFile::mkdir(FatFile* parent, const char* path, bool pFlag) { fname_t fname; FatFile tmpDir; - if (isOpen() || !parent->isDir()) { + if (isOpen() || !parent || !parent->isDir()) { DBG_FAIL_MACRO; goto fail; } @@ -306,7 +306,7 @@ bool FatFile::mkdir(FatFile* parent, fname_t* fname) { dir_t* dir; cache_t* pc; - if (!parent->isDir()) { + if (!parent || !parent->isDir()) { DBG_FAIL_MACRO; goto fail; } @@ -379,7 +379,7 @@ bool FatFile::open(FatFile* dirFile, const char* path, oflag_t oflag) { fname_t fname; // error if already open - if (isOpen() || !dirFile->isDir()) { + if (isOpen() || !dirFile || !dirFile->isDir()) { DBG_FAIL_MACRO; goto fail; } @@ -425,7 +425,7 @@ bool FatFile::open(FatFile* dirFile, uint16_t index, oflag_t oflag) { ldir_t*ldir; // Error if already open. - if (isOpen() || !dirFile->isDir()) { + if (isOpen() || !dirFile || !dirFile->isDir()) { DBG_FAIL_MACRO; goto fail; } @@ -581,8 +581,9 @@ bool FatFile::openNext(FatFile* dirFile, oflag_t oflag) { uint8_t lfnOrd = 0; uint16_t index; - // Check for not open and valid directory.. - if (isOpen() || !dirFile->isDir() || (dirFile->curPosition() & 0X1F)) { + // Check for not open and valid directory. + if (isOpen() || !dirFile || !dirFile->isDir() + || (dirFile->curPosition() & 0X1F)) { DBG_FAIL_MACRO; goto fail; } @@ -643,7 +644,7 @@ bool FatFile::openParent(FatFile* dirFile) { uint32_t ddc; cache_t* cb; - if (isOpen() || !dirFile->isOpen()) { + if (isOpen() || !dirFile || !dirFile->isOpen()) { goto fail; } if (dirFile->m_dirCluster == 0) { @@ -1235,8 +1236,8 @@ bool FatFile::timestamp(FatFile* file) { dir_t* dir; dir_t srcDir; - // most be files get timestamps - if (!isFile() || !file->isFile() || !file->dirEntry(&srcDir)) { + // must be files to get timestamps + if (!isFile() || !file || !file->isFile() || !file->dirEntry(&srcDir)) { DBG_FAIL_MACRO; goto fail; } diff --git a/src/FatLib/FatFile.h b/src/FatLib/FatFile.h index 00654277..f5e1fc56 100644 --- a/src/FatLib/FatFile.h +++ b/src/FatLib/FatFile.h @@ -835,7 +835,7 @@ class FatFile { * \return true for success else false. */ static bool setCwd(FatFile* dir) { - if (!dir->isDir()) { + if (!dir || !dir->isDir()) { return false; } m_cwd = dir; diff --git a/src/FatLib/FatFileLFN.cpp b/src/FatLib/FatFileLFN.cpp index c8334a56..5c7ba306 100644 --- a/src/FatLib/FatFileLFN.cpp +++ b/src/FatLib/FatFileLFN.cpp @@ -303,7 +303,7 @@ bool FatFile::open(FatFile* dirFile, fname_t* fname, oflag_t oflag) { ldir_t* ldir; size_t len = fname->len; - if (!dirFile->isDir() || isOpen()) { + if (!dirFile || dirFile->isDir() || isOpen()) { DBG_FAIL_MACRO; goto fail; }