diff --git a/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp b/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp index b1ce86e05c..29c407bd15 100644 --- a/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp +++ b/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp @@ -107,7 +107,8 @@ static unsigned int local_math_power(int base, int exp); //*********************** SPIFBlockDevice::SPIFBlockDevice( PinName mosi, PinName miso, PinName sclk, PinName csel, int freq) - : _spi(mosi, miso, sclk), _cs(csel), _device_size_bytes(0), _is_initialized(false), _init_ref_count(0) + : _spi(mosi, miso, sclk), _cs(csel), _read_instruction(0), _prog_instruction(0), _erase_instruction(0), + _erase4k_inst(0), _page_size_bytes(0), _device_size_bytes(0), _init_ref_count(0), _is_initialized(false) { _address_size = SPIF_ADDR_SIZE_3_BYTES; // Initial SFDP read tables are read with 8 dummy cycles @@ -205,7 +206,7 @@ int SPIFBlockDevice::init() _region_high_boundary[0] = _device_size_bytes - 1; if ((sector_map_table_addr != 0) && (0 != sector_map_table_size)) { - debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: init - Parsing Sector Map Table - addr: 0x%lxh, Size: %d", sector_map_table_addr, + debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: init - Parsing Sector Map Table - addr: 0x%" PRIx32 "h, Size: %d", sector_map_table_addr, sector_map_table_size); if (0 != _sfdp_parse_sector_map_table(sector_map_table_addr, sector_map_table_size)) { tr_error("init - Parse Sector Map Table Failed"); @@ -296,7 +297,7 @@ int SPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size) uint32_t offset = 0; uint32_t chunk = 0; - debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: program - Buff: 0x%lxh, addr: %llu, size: %llu", (uint32_t)buffer, addr, size); + debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: program - Buff: 0x%" PRIx32 "h, addr: %llu, size: %llu", (uint32_t)buffer, addr, size); while (size > 0) { @@ -352,6 +353,10 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) int status = SPIF_BD_ERROR_OK; // Find region of erased address int region = _utils_find_addr_region(addr); + if (region < 0) { + tr_error("no region found for address %llu", addr); + return SPIF_BD_ERROR_INVALID_ERASE_PARAMS; + } // Erase Types of selected region uint8_t bitfield = _region_erase_types_bitfield[region]; @@ -377,7 +382,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size) offset = addr % _erase_type_size_arr[type]; chunk = ((offset + size) < _erase_type_size_arr[type]) ? size : (_erase_type_size_arr[type] - offset); - debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: erase - addr: %llu, size:%d, Inst: 0x%xh, chunk: %lu , ", + debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: erase - addr: %llu, size:%d, Inst: 0x%xh, chunk: %" PRIu32 " , ", addr, size, cur_erase_inst, chunk); debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "DEBUG: erase - Region: %d, Type:%d", region, type); @@ -439,7 +444,7 @@ bd_size_t SPIFBlockDevice::get_erase_size() const } // Find minimal erase size supported by the region to which the address belongs to -bd_size_t SPIFBlockDevice::get_erase_size(bd_addr_t addr) +bd_size_t SPIFBlockDevice::get_erase_size(bd_addr_t addr) const { // Find region of current address int region = _utils_find_addr_region(addr); @@ -693,7 +698,7 @@ int SPIFBlockDevice::_sfdp_parse_basic_param_table(uint32_t basic_table_addr, si (param_table[5] << 8) | param_table[4]); _device_size_bytes = (density_bits + 1) / 8; - tr_debug("Density bits: %ld , device size: %llu bytes", density_bits, _device_size_bytes); + tr_debug("Density bits: %" PRIu32 " , device size: %llu bytes", density_bits, _device_size_bytes); // Set Default read/program/erase Instructions _read_instruction = SPIF_READ; @@ -998,7 +1003,7 @@ int SPIFBlockDevice::_set_write_enable() /*********************************************/ /************* Utility Functions *************/ /*********************************************/ -int SPIFBlockDevice::_utils_find_addr_region(bd_size_t offset) +int SPIFBlockDevice::_utils_find_addr_region(bd_size_t offset) const { //Find the region to which the given offset belong to if ((offset > _device_size_bytes) || (_regions_count == 0)) { diff --git a/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h b/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h index e0e68c15e3..c7ab3b3987 100644 --- a/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h +++ b/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h @@ -170,7 +170,7 @@ public: * @return Size of minimal erase sector size, in given address region, in bytes * @note Must be a multiple of the program size */ - virtual mbed::bd_size_t get_erase_size(mbed::bd_addr_t addr); + virtual mbed::bd_size_t get_erase_size(mbed::bd_addr_t addr) const; /** Get the value of storage byte after it was erased * @@ -227,7 +227,7 @@ private: /* Utilities Functions */ /***********************/ // Find the region to which the given offset belongs to - int _utils_find_addr_region(bd_size_t offset); + int _utils_find_addr_region(bd_size_t offset) const; // Iterate on all supported Erase Types of the Region to which the offset belongs to. // Iterates from highest type to lowest diff --git a/features/netsocket/nsapi_dns.cpp b/features/netsocket/nsapi_dns.cpp index e8f245b7ca..bb8d7c33df 100644 --- a/features/netsocket/nsapi_dns.cpp +++ b/features/netsocket/nsapi_dns.cpp @@ -717,7 +717,7 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const if (!dns_timer_running) { if (nsapi_dns_call_in(query->call_in_cb, DNS_TIMER_TIMEOUT, mbed::callback(nsapi_dns_query_async_timeout)) != NSAPI_ERROR_OK) { - delete query->host; + delete[] query->host; delete query; dns_mutex->unlock(); return NSAPI_ERROR_NO_MEMORY; diff --git a/features/storage/blockdevice/BufferedBlockDevice.cpp b/features/storage/blockdevice/BufferedBlockDevice.cpp index 4eeb7bf2e1..eaa188532a 100644 --- a/features/storage/blockdevice/BufferedBlockDevice.cpp +++ b/features/storage/blockdevice/BufferedBlockDevice.cpp @@ -28,7 +28,7 @@ static inline uint32_t align_down(bd_size_t val, bd_size_t size) } BufferedBlockDevice::BufferedBlockDevice(BlockDevice *bd) - : _bd(bd), _bd_program_size(0), _bd_read_size(0), _write_cache_addr(0), _write_cache_valid(false), + : _bd(bd), _bd_program_size(0), _bd_read_size(0), _bd_size(0), _write_cache_addr(0), _write_cache_valid(false), _write_cache(0), _read_buf(0), _init_ref_count(0), _is_initialized(false) { } diff --git a/features/storage/filesystem/fat/ChaN/ff.cpp b/features/storage/filesystem/fat/ChaN/ff.cpp index 7e4561d809..e98bb3e43c 100644 --- a/features/storage/filesystem/fat/ChaN/ff.cpp +++ b/features/storage/filesystem/fat/ChaN/ff.cpp @@ -2719,7 +2719,7 @@ void get_fileinfo ( if (wc == 0) { di = 0; break; } /* Buffer overflow? */ di += wc; #else /* ANSI/OEM output */ - fno->altname[di++] = (TCHAR)wc; /* Store it without any conversion */ + if (di <= FF_SFN_BUF) fno->altname[di++] = (TCHAR)wc; /* Store it without any conversion */ #endif } fno->altname[di] = 0; /* Terminate the SFN (null string means SFN is invalid) */ @@ -4938,7 +4938,7 @@ FRESULT f_mkdir ( res = sync_fs(fs); } } else { - remove_chain(&dj.obj, dcl, 0); /* Could not register, remove cluster chain */ + res = remove_chain(&dj.obj, dcl, 0); /* Could not register, remove cluster chain */ } } FREE_NAMBUF(); @@ -4967,7 +4967,8 @@ FRESULT f_rename ( DEF_NAMBUF - get_ldnumber(&path_new); /* Snip the drive number of new name off */ + int vol = get_ldnumber(&path_new); /* Snip the drive number of new name off */ + if (vol < 0) return FR_INVALID_DRIVE; res = find_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */ if (res == FR_OK) { djo.obj.fs = fs; diff --git a/features/storage/filesystem/fat/FATFileSystem.cpp b/features/storage/filesystem/fat/FATFileSystem.cpp index 297ac109e9..415aac41ad 100644 --- a/features/storage/filesystem/fat/FATFileSystem.cpp +++ b/features/storage/filesystem/fat/FATFileSystem.cpp @@ -282,6 +282,7 @@ extern "C" DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) FATFileSystem::FATFileSystem(const char *name, BlockDevice *bd) : FileSystem(name), _id(-1) { + _fs = { 0 }; if (bd) { mount(bd); } diff --git a/features/storage/filesystem/littlefs/LittleFileSystem.cpp b/features/storage/filesystem/littlefs/LittleFileSystem.cpp index 23c749c1dd..34ee5f6093 100644 --- a/features/storage/filesystem/littlefs/LittleFileSystem.cpp +++ b/features/storage/filesystem/littlefs/LittleFileSystem.cpp @@ -149,6 +149,9 @@ LittleFileSystem::LittleFileSystem(const char *name, BlockDevice *bd, , _prog_size(prog_size) , _block_size(block_size) , _lookahead(lookahead) + , _lfs() + , _config() + , _bd(NULL) { if (bd) { mount(bd); diff --git a/features/storage/filesystem/littlefs/littlefs/lfs.c b/features/storage/filesystem/littlefs/littlefs/lfs.c index 40d2b86ddc..477addb124 100644 --- a/features/storage/filesystem/littlefs/littlefs/lfs.c +++ b/features/storage/filesystem/littlefs/littlefs/lfs.c @@ -924,7 +924,7 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { // build up new directory lfs_alloc_ack(lfs); - lfs_dir_t dir; + lfs_dir_t dir = { 0 }; err = lfs_dir_alloc(lfs, &dir); if (err) { return err; @@ -2106,7 +2106,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { lfs_alloc_ack(lfs); // create superblock dir - lfs_dir_t superdir; + lfs_dir_t superdir = { 0 }; err = lfs_dir_alloc(lfs, &superdir); if (err) { goto cleanup; diff --git a/features/storage/kvstore/filesystemstore/FileSystemStore.cpp b/features/storage/kvstore/filesystemstore/FileSystemStore.cpp index 01ac336e3e..36f8d138d8 100644 --- a/features/storage/kvstore/filesystemstore/FileSystemStore.cpp +++ b/features/storage/kvstore/filesystemstore/FileSystemStore.cpp @@ -62,7 +62,8 @@ static char *string_ndup(const char *src, size_t size); // Class Functions FileSystemStore::FileSystemStore(FileSystem *fs) : _fs(fs), - _is_initialized(false) + _is_initialized(false), _cfg_fs_path(NULL), _cfg_fs_path_size(0), + _full_path_key(NULL), _cur_inc_data_size(0), _cur_inc_set_handle(NULL) { } @@ -532,7 +533,7 @@ int FileSystemStore::iterator_next(iterator_t it, char *key, size_t key_size) key_it = (key_iterator_handle_t *)it; - if (key_name_size < strlen(key_it->prefix)) { + if ((key_it->prefix != NULL) && (key_name_size < strlen(key_it->prefix))) { status = MBED_ERROR_INVALID_SIZE; goto exit_point; } @@ -577,9 +578,8 @@ int FileSystemStore::iterator_close(iterator_t it) delete[] key_it->prefix; } - ((Dir *)(key_it->dir_handle))->close(); - if (key_it->dir_handle != NULL) { + ((Dir *)(key_it->dir_handle))->close(); delete ((Dir *)(key_it->dir_handle)); } delete key_it; diff --git a/features/storage/kvstore/kv_map/KVMap.cpp b/features/storage/kvstore/kv_map/KVMap.cpp index e86f8bee04..5bcf7fd608 100644 --- a/features/storage/kvstore/kv_map/KVMap.cpp +++ b/features/storage/kvstore/kv_map/KVMap.cpp @@ -65,7 +65,7 @@ int KVMap::attach(const char *partition_name, kvstore_config_t *kv_config) goto exit; } - kv_partition_name = new char[strlen(partition_name + 1)]; + kv_partition_name = new char[strlen(partition_name) + 1]; strcpy(kv_partition_name, partition_name); _kv_map_table[_kv_num_attached_kvs].partition_name = kv_partition_name; _kv_map_table[_kv_num_attached_kvs].kv_config = kv_config; @@ -251,10 +251,7 @@ KVStore *KVMap::get_internal_kv_instance(const char *name) size_t key_index = 0; int ret = config_lookup(name, &kv_config, &key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } -exit: + _mutex->unlock(); return ret != MBED_SUCCESS ? NULL : kv_config->internal_store; @@ -269,10 +266,7 @@ KVStore *KVMap::get_external_kv_instance(const char *name) size_t key_index = 0; int ret = config_lookup(name, &kv_config, &key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } -exit: + _mutex->unlock(); return ret != MBED_SUCCESS ? NULL : kv_config->external_store; @@ -287,10 +281,7 @@ KVStore *KVMap::get_main_kv_instance(const char *name) size_t key_index = 0; int ret = config_lookup(name, &kv_config, &key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } -exit: + _mutex->unlock(); return ret != MBED_SUCCESS ? NULL : kv_config->kvstore_main_instance; @@ -305,10 +296,7 @@ BlockDevice *KVMap::get_internal_blockdevice_instance(const char *name) size_t key_index = 0; int ret = config_lookup(name, &kv_config, &key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } -exit: + _mutex->unlock(); return ret != MBED_SUCCESS ? NULL : kv_config->internal_bd; @@ -323,10 +311,7 @@ BlockDevice *KVMap::get_external_blockdevice_instance(const char *name) size_t key_index = 0; int ret = config_lookup(name, &kv_config, &key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } -exit: + _mutex->unlock(); return ret != MBED_SUCCESS ? NULL : kv_config->external_bd; @@ -341,10 +326,7 @@ FileSystem *KVMap::get_external_filesystem_instance(const char *name) size_t key_index = 0; int ret = config_lookup(name, &kv_config, &key_index); - if (ret != MBED_SUCCESS) { - goto exit; - } -exit: + _mutex->unlock(); return ret != MBED_SUCCESS ? NULL : kv_config->external_fs; diff --git a/features/storage/kvstore/securestore/SecureStore.cpp b/features/storage/kvstore/securestore/SecureStore.cpp index 17207481a7..e291316cb9 100644 --- a/features/storage/kvstore/securestore/SecureStore.cpp +++ b/features/storage/kvstore/securestore/SecureStore.cpp @@ -109,7 +109,7 @@ int encrypt_decrypt_start(mbedtls_aes_context &enc_aes_ctx, uint8_t *iv, const c int encrypt_decrypt_data(mbedtls_aes_context &enc_aes_ctx, const uint8_t *in_buf, uint8_t *out_buf, uint32_t chunk_size, uint8_t *ctr_buf, size_t &aes_offs) { - uint8_t stream_block[enc_block_size]; + uint8_t stream_block[enc_block_size] = { 0 }; return mbedtls_aes_crypt_ctr(&enc_aes_ctx, chunk_size, &aes_offs, ctr_buf, stream_block, in_buf, out_buf); diff --git a/features/storage/kvstore/tdbstore/TDBStore.cpp b/features/storage/kvstore/tdbstore/TDBStore.cpp index 0ab8dcecb3..d4d2dcc1b8 100644 --- a/features/storage/kvstore/tdbstore/TDBStore.cpp +++ b/features/storage/kvstore/tdbstore/TDBStore.cpp @@ -127,6 +127,12 @@ TDBStore::TDBStore(BlockDevice *bd) : _ram_table(0), _max_keys(0), _master_record_size(0), _is_initialized(false), _active_area(0), _active_area_version(0), _size(0), _prog_size(0), _work_buf(0), _key_buf(0), _variant_bd_erase_unit_size(false), _inc_set_handle(0) { + for (int i = 0; i < _num_areas; i++) { + _area_params[i] = { 0 }; + } + for (int i = 0; i < _max_open_iterators; i++) { + _iterator_table[i] = { 0 }; + } } TDBStore::~TDBStore() @@ -391,8 +397,8 @@ int TDBStore::set_start(set_handle_t *handle, const char *key, size_t final_data uint32_t create_flags) { int ret; - uint32_t offset; - uint32_t hash, ram_table_ind; + uint32_t offset = 0; + uint32_t hash = 0, ram_table_ind = 0; inc_set_handle_t *ih; bool need_gc = false; diff --git a/features/storage/nvstore/source/nvstore.cpp b/features/storage/nvstore/source/nvstore.cpp index 2e14109df3..76f375fec4 100644 --- a/features/storage/nvstore/source/nvstore.cpp +++ b/features/storage/nvstore/source/nvstore.cpp @@ -148,6 +148,9 @@ NVStore::NVStore() : _init_done(0), _init_attempts(0), _active_area(0), _max_key _active_area_version(0), _free_space_offset(0), _size(0), _mutex(0), _offset_by_key(0), _flash(0), _min_prog_size(0), _page_buf(0) { + for (int i = 0; i < NVSTORE_NUM_AREAS; i++) { + _flash_area_params[i] = { 0 }; + } } NVStore::~NVStore()