Skip to content

Commit fb9ef27

Browse files
committed
fixed solosky#219, check file name restriction before create file
1 parent 9bf5fba commit fb9ef27

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

fw/application/src/mod/vfs/vfs_driver_spiffs.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ static s32_t vfs_spiffs_map_error_code(s32_t err) {
4949
return VFS_ERR_FAIL;
5050
}
5151

52+
static int32_t vfs_check_file_path(const char* file_path){
53+
if(strlen(file_path) >= VFS_MAX_PATH_LEN){
54+
return VFS_ERR_MAXNM;
55+
}
56+
const char* basename;
57+
size_t length;
58+
cwk_path_get_basename(file_path, &basename, &length);
59+
if(length <= 0 || length >= VFS_MAX_NAME_LEN){
60+
return VFS_ERR_MAXNM;
61+
}
62+
return VFS_OK;
63+
}
64+
5265
static s32_t spiffs_block_read(u32_t addr, u32_t size, u8_t *dst) { return hal_spi_flash_read(addr, dst, size); }
5366

5467
static s32_t spiffs_block_write(u32_t addr, u32_t size, u8_t *src) { return hal_spi_flash_prog(addr, src, size); }
@@ -277,8 +290,12 @@ int32_t vfs_spiffs_create_dir(const char *dir) {
277290
char path[VFS_MAX_PATH_LEN];
278291

279292
NRF_LOG_INFO("create dir %s\n", nrf_log_push(dir));
293+
int res = vfs_check_file_path(dir);
294+
if(res != VFS_OK){
295+
return res;
296+
}
280297
snprintf(path, sizeof(path), "%s/%s", dir, VFS_SPIFFS_FOLDER_NAME);
281-
int res = SPIFFS_creat(&fs, path, 0);
298+
res = SPIFFS_creat(&fs, path, 0);
282299
return vfs_spiffs_map_error_code(res);
283300
}
284301

@@ -318,6 +335,16 @@ int32_t vfs_spiffs_rename_dir_internal(const char *dir_name, const char *new_dir
318335
vfs_spiffs_dir_t *p_dir = &dir;
319336
int32_t err_code = VFS_OK;
320337

338+
int ret = vfs_check_file_path(dir_name);
339+
if( ret != VFS_OK){
340+
return ret;
341+
}
342+
343+
int ret2 = vfs_check_file_path(new_dir_name);
344+
if( ret2 != VFS_OK){
345+
return ret;
346+
}
347+
321348
p_dir->pe = &p_dir->e;
322349
cwalk_dir_prefix_match(p_dir->dir, dir_name);
323350

@@ -368,6 +395,10 @@ int32_t vfs_spiffs_rename_dir(const char *dir_name, const char *new_dir_name) {
368395

369396
/**file operations*/
370397
int32_t vfs_spiffs_open_file(const char *file, vfs_file_t *fd, uint32_t flags) {
398+
int ret = vfs_check_file_path(file);
399+
if( ret != VFS_OK){
400+
return ret;
401+
}
371402
fd->handle = SPIFFS_open(&fs, file, flags, 0);
372403
if (fd->handle < 0) {
373404
return vfs_spiffs_map_error_code(fd->handle);
@@ -431,6 +462,12 @@ int32_t vfs_spiffs_update_file_meta(const char *file, void *meta, size_t meta_si
431462
int32_t vfs_spiffs_write_file_data(const char *file, void *buff, size_t buff_size) {
432463

433464
NRF_LOG_INFO("write file data %s\n", nrf_log_push(file));
465+
466+
int ret = vfs_check_file_path(file);
467+
if( ret != VFS_OK){
468+
return ret;
469+
}
470+
434471
spiffs_file fd = SPIFFS_open(&fs, file, SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
435472
if (fd < 0) {
436473
return vfs_spiffs_map_error_code(fd);

0 commit comments

Comments
 (0)