Skip to content

Commit

Permalink
SPIFFS: check if path length is valid (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Dec 10, 2015
1 parent 703aaf6 commit e7024fb
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions cores/esp8266/spiffs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ extern int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src);
extern int32_t spiffs_hal_erase(uint32_t addr, uint32_t size);
extern int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst);

int getSpiffsMode(OpenMode openMode, AccessMode accessMode);
static int getSpiffsMode(OpenMode openMode, AccessMode accessMode);
static bool isSpiffsFilenameValid(const char* name);

class SPIFFSFileImpl;
class SPIFFSDirImpl;


class SPIFFSImpl : public FSImpl {
public:
SPIFFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
Expand All @@ -63,6 +65,14 @@ class SPIFFSImpl : public FSImpl {
DirImplPtr openDir(const char* path) override;

bool rename(const char* pathFrom, const char* pathTo) override {
if (!isSpiffsFilenameValid(pathFrom)) {
DEBUGV("SPIFFSImpl::rename: invalid pathFrom=`%s`\r\n", path);
return false;
}
if (!isSpiffsFilenameValid(pathTo)) {
DEBUGV("SPIFFSImpl::rename: invalid pathTo=`%s` \r\n", path);
return false;
}
auto rc = SPIFFS_rename(&_fs, pathFrom, pathTo);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc,
Expand All @@ -86,6 +96,10 @@ class SPIFFSImpl : public FSImpl {
}

bool remove(const char* path) override {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::remove: invalid path=`%s`\r\n", path);
return false;
}
auto rc = SPIFFS_remove(&_fs, path);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path);
Expand Down Expand Up @@ -229,7 +243,7 @@ class SPIFFSImpl : public FSImpl {
std::unique_ptr<uint8_t[]> _cacheBuf;
};

#define CHECKFD() while (_fd == 0) { DEBUGV("SPIFFSFileImpl(%d) _fd == 0\r\n", __LINE__); abort(); }
#define CHECKFD() while (_fd == 0) { panic(); }

class SPIFFSFileImpl : public FileImpl {
public:
Expand Down Expand Up @@ -408,6 +422,10 @@ class SPIFFSDirImpl : public DirImpl {


FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::open: invalid path=`%s` \r\n", path);
return FileImplPtr();
}
int mode = getSpiffsMode(openMode, accessMode);
int fd = SPIFFS_open(&_fs, path, mode, 0);
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
Expand All @@ -430,12 +448,20 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc
}

bool SPIFFSImpl::exists(const char* path) {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::exists: invalid path=`%s` \r\n", path);
return false;
}
spiffs_stat stat;
int rc = SPIFFS_stat(&_fs, path, &stat);
return rc == SPIFFS_OK;
}

DirImplPtr SPIFFSImpl::openDir(const char* path) {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::openDir: invalid path=`%s` \r\n", path);
return DirImplPtr();
}
spiffs_DIR dir;
spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir);
if (!result) {
Expand All @@ -445,7 +471,7 @@ DirImplPtr SPIFFSImpl::openDir(const char* path) {
return std::make_shared<SPIFFSDirImpl>(path, this, dir);
}

int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
static int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
int mode = 0;
if (openMode & OM_CREATE) {
mode |= SPIFFS_CREAT;
Expand All @@ -465,6 +491,13 @@ int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
return mode;
}

static bool isSpiffsFilenameValid(const char* name) {
if (name == nullptr)
return false;
auto len = strlen(name);
return len > 0 && len <= SPIFFS_OBJ_NAME_LEN;
}

// these symbols should be defined in the linker script for each flash layout
extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end;
Expand Down

0 comments on commit e7024fb

Please sign in to comment.