@@ -177,9 +177,7 @@ void NVStore::set_max_keys(uint16_t num_keys)
177
177
{
178
178
uint16_t key = 0 , old_max_keys = 0 ;
179
179
180
- MBED_ASSERT (num_keys < get_max_possible_keys ());
181
-
182
- if (num_keys < NVSTORE_NUM_PREDEFINED_KEYS) {
180
+ if (num_keys < NVSTORE_NUM_PREDEFINED_KEYS || num_keys >= get_max_possible_keys ()) {
183
181
return ;
184
182
}
185
183
@@ -196,6 +194,7 @@ void NVStore::set_max_keys(uint16_t num_keys)
196
194
if (num_keys < _max_keys) {
197
195
for (key = num_keys; key < _max_keys; key++) {
198
196
if (_offset_by_key[key] != 0 ) {
197
+ _mutex->unlock ();
199
198
return ;
200
199
}
201
200
}
@@ -212,7 +211,12 @@ void NVStore::set_max_keys(uint16_t num_keys)
212
211
// Reallocate _offset_by_key with new size
213
212
uint32_t *old_offset_by_key = (uint32_t *) _offset_by_key;
214
213
uint32_t *new_offset_by_key = new uint32_t [_max_keys];
214
+
215
215
MBED_ASSERT (new_offset_by_key);
216
+ if (!new_offset_by_key) {
217
+ _mutex->unlock ();
218
+ return ;
219
+ }
216
220
217
221
// Copy old content to new table
218
222
memset (new_offset_by_key, 0 , sizeof (uint32_t ) * _max_keys);
@@ -249,6 +253,10 @@ void NVStore::calc_validate_area_params()
249
253
size_t flash_addr;
250
254
size_t sector_size;
251
255
256
+ if (flash_size == 0 ) {
257
+ return ;
258
+ }
259
+
252
260
int area = 0 ;
253
261
size_t left_size = flash_size;
254
262
@@ -293,7 +301,6 @@ void NVStore::calc_validate_area_params()
293
301
_flash_area_params[0 ].size = 0 ;
294
302
_flash_area_params[1 ].size = 0 ;
295
303
while (area >= 0 ) {
296
- MBED_ASSERT (flash_addr > flash_start_addr);
297
304
sector_size = _flash->get_sector_size (flash_addr - 1 );
298
305
flash_addr -= sector_size;
299
306
_flash_area_params[area].size += sector_size;
@@ -827,8 +834,9 @@ int NVStore::init()
827
834
// Check if we are on internal memory && try to set the internal memory for TDBStore use.
828
835
ret = avoid_conflict_nvstore_tdbstore (NVSTORE);
829
836
// NVstore in internal memory can not be initialize when TDBStore is in use
830
- MBED_ASSERT (ret != MBED_ERROR_ALREADY_INITIALIZED);
831
-
837
+ if (ret == MBED_ERROR_ALREADY_INITIALIZED) {
838
+ return ret;
839
+ }
832
840
833
841
// This handles the case that init function is called by more than one thread concurrently.
834
842
// Only the one who gets the value of 1 in _init_attempts_val will proceed, while others will
@@ -842,17 +850,23 @@ int NVStore::init()
842
850
}
843
851
844
852
_mutex = new PlatformMutex;
845
- MBED_ASSERT (_mutex);
853
+ if (!_mutex) {
854
+ return NVSTORE_OS_ERROR;
855
+ }
846
856
847
857
_size = (uint32_t ) -1 ;
848
858
_flash = new mbed::FlashIAP;
849
- MBED_ASSERT (_flash);
859
+ if (!_flash) {
860
+ return NVSTORE_OS_ERROR;
861
+ }
850
862
_flash->init ();
851
863
852
864
_min_prog_size = std::max (_flash->get_page_size (), (uint32_t )sizeof (nvstore_record_header_t ));
853
865
if (_min_prog_size > sizeof (nvstore_record_header_t )) {
854
866
_page_buf = new uint8_t [_min_prog_size];
855
- MBED_ASSERT (_page_buf);
867
+ if (!_page_buf) {
868
+ return NVSTORE_OS_ERROR;
869
+ }
856
870
}
857
871
858
872
calc_validate_area_params ();
@@ -869,7 +883,9 @@ int NVStore::init()
869
883
// Find start of empty space at the end of the area. This serves for both
870
884
// knowing whether the area is empty and for the record traversal at the end.
871
885
ret = calc_empty_space (area, free_space_offset_of_area[area]);
872
- MBED_ASSERT (!ret);
886
+ if (ret) {
887
+ return ret;
888
+ }
873
889
874
890
if (!free_space_offset_of_area[area]) {
875
891
area_state[area] = NVSTORE_AREA_STATE_EMPTY;
@@ -881,7 +897,9 @@ int NVStore::init()
881
897
ret = read_record (area, 0 , sizeof (master_rec), &master_rec,
882
898
actual_size, 0 , valid,
883
899
key, flags, owner, next_offset);
884
- MBED_ASSERT ((ret == NVSTORE_SUCCESS) || (ret == NVSTORE_BUFF_TOO_SMALL));
900
+ if ((ret != NVSTORE_SUCCESS) && (ret != NVSTORE_BUFF_TOO_SMALL)) {
901
+ return ret;
902
+ }
885
903
if (ret == NVSTORE_BUFF_TOO_SMALL) {
886
904
// Buf too small error means that we have a corrupt master record -
887
905
// treat it as such
@@ -891,7 +909,9 @@ int NVStore::init()
891
909
// We have a non valid master record, in a non-empty area. Just erase the area.
892
910
if ((!valid) || (key != master_record_key)) {
893
911
ret = flash_erase_area (area);
894
- MBED_ASSERT (!ret);
912
+ if (ret) {
913
+ return ret;
914
+ }
895
915
area_state[area] = NVSTORE_AREA_STATE_EMPTY;
896
916
continue ;
897
917
}
@@ -914,7 +934,9 @@ int NVStore::init()
914
934
}
915
935
916
936
_offset_by_key = new uint32_t [_max_keys];
917
- MBED_ASSERT (_offset_by_key);
937
+ if (!_offset_by_key) {
938
+ return NVSTORE_OS_ERROR;
939
+ }
918
940
919
941
for (key = 0 ; key < _max_keys; key++) {
920
942
_offset_by_key[key] = 0 ;
@@ -924,7 +946,9 @@ int NVStore::init()
924
946
if ((area_state[0 ] == NVSTORE_AREA_STATE_EMPTY) && (area_state[1 ] == NVSTORE_AREA_STATE_EMPTY)) {
925
947
_active_area = 0 ;
926
948
ret = write_master_record (_active_area, 1 , _free_space_offset);
927
- MBED_ASSERT (ret == NVSTORE_SUCCESS);
949
+ if (ret != NVSTORE_SUCCESS) {
950
+ return ret;
951
+ }
928
952
_init_done = 1 ;
929
953
return NVSTORE_SUCCESS;
930
954
}
@@ -939,15 +963,19 @@ int NVStore::init()
939
963
}
940
964
_active_area_version = versions[_active_area];
941
965
ret = flash_erase_area (1 - _active_area);
942
- MBED_ASSERT (!ret);
966
+ if (ret) {
967
+ return ret;
968
+ }
943
969
}
944
970
945
971
// Traverse area until reaching the empty space at the end or until reaching a faulty record
946
972
while (_free_space_offset < free_space_offset_of_area[_active_area]) {
947
973
ret = read_record (_active_area, _free_space_offset, 0 , NULL ,
948
974
actual_size, 1 , valid,
949
975
key, flags, owner, next_offset);
950
- MBED_ASSERT (ret == NVSTORE_SUCCESS);
976
+ if (ret != NVSTORE_SUCCESS) {
977
+ return ret;
978
+ }
951
979
952
980
// In case we have a faulty record, this probably means that the system crashed when written.
953
981
// Perform a garbage collection, to make the other area valid.
0 commit comments