mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
SPIFFS: check if path length is valid (#1089)
This commit is contained in:
parent
703aaf60f4
commit
e7024fb5b4
@ -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_erase(uint32_t addr, uint32_t size);
|
||||||
extern int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst);
|
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 SPIFFSFileImpl;
|
||||||
class SPIFFSDirImpl;
|
class SPIFFSDirImpl;
|
||||||
|
|
||||||
|
|
||||||
class SPIFFSImpl : public FSImpl {
|
class SPIFFSImpl : public FSImpl {
|
||||||
public:
|
public:
|
||||||
SPIFFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
|
SPIFFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
|
||||||
@ -63,6 +65,14 @@ public:
|
|||||||
DirImplPtr openDir(const char* path) override;
|
DirImplPtr openDir(const char* path) override;
|
||||||
|
|
||||||
bool rename(const char* pathFrom, const char* pathTo) 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);
|
auto rc = SPIFFS_rename(&_fs, pathFrom, pathTo);
|
||||||
if (rc != SPIFFS_OK) {
|
if (rc != SPIFFS_OK) {
|
||||||
DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc,
|
DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc,
|
||||||
@ -86,6 +96,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool remove(const char* path) override {
|
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);
|
auto rc = SPIFFS_remove(&_fs, path);
|
||||||
if (rc != SPIFFS_OK) {
|
if (rc != SPIFFS_OK) {
|
||||||
DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path);
|
DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path);
|
||||||
@ -229,7 +243,7 @@ protected:
|
|||||||
std::unique_ptr<uint8_t[]> _cacheBuf;
|
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 {
|
class SPIFFSFileImpl : public FileImpl {
|
||||||
public:
|
public:
|
||||||
@ -408,6 +422,10 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) {
|
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 mode = getSpiffsMode(openMode, accessMode);
|
||||||
int fd = SPIFFS_open(&_fs, path, mode, 0);
|
int fd = SPIFFS_open(&_fs, path, mode, 0);
|
||||||
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
|
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
|
||||||
@ -430,12 +448,20 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SPIFFSImpl::exists(const char* path) {
|
bool SPIFFSImpl::exists(const char* path) {
|
||||||
|
if (!isSpiffsFilenameValid(path)) {
|
||||||
|
DEBUGV("SPIFFSImpl::exists: invalid path=`%s` \r\n", path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
spiffs_stat stat;
|
spiffs_stat stat;
|
||||||
int rc = SPIFFS_stat(&_fs, path, &stat);
|
int rc = SPIFFS_stat(&_fs, path, &stat);
|
||||||
return rc == SPIFFS_OK;
|
return rc == SPIFFS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
DirImplPtr SPIFFSImpl::openDir(const char* path) {
|
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 dir;
|
||||||
spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir);
|
spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
@ -445,7 +471,7 @@ DirImplPtr SPIFFSImpl::openDir(const char* path) {
|
|||||||
return std::make_shared<SPIFFSDirImpl>(path, this, dir);
|
return std::make_shared<SPIFFSDirImpl>(path, this, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
|
static int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
|
||||||
int mode = 0;
|
int mode = 0;
|
||||||
if (openMode & OM_CREATE) {
|
if (openMode & OM_CREATE) {
|
||||||
mode |= SPIFFS_CREAT;
|
mode |= SPIFFS_CREAT;
|
||||||
@ -465,6 +491,13 @@ int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
|
|||||||
return mode;
|
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
|
// these symbols should be defined in the linker script for each flash layout
|
||||||
extern "C" uint32_t _SPIFFS_start;
|
extern "C" uint32_t _SPIFFS_start;
|
||||||
extern "C" uint32_t _SPIFFS_end;
|
extern "C" uint32_t _SPIFFS_end;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user