Skip to content

Commit f8244a3

Browse files
review comments, init partly moved to restore, restore setting enabled, null check on filepath
1 parent 579cb5e commit f8244a3

File tree

1 file changed

+61
-47
lines changed

1 file changed

+61
-47
lines changed

features/FEATURE_BLE/source/generic/FileSecurityDb.cpp

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,10 @@ FileSecurityDb::FileSecurityDb(FILE *db_file)
7878
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
7979

8080
/* restore if requested */
81-
bool restore;
82-
if ((fread(&restore, sizeof(bool), 1, _db_file) == 1) && restore) {
83-
fseek(_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
84-
fread(&_local_identity, sizeof(_local_identity), 1, _db_file);
85-
86-
fseek(_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
87-
fread(&_local_csrk, sizeof(_local_csrk), 1, _db_file);
88-
89-
fseek(_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
90-
fread(&_local_sign_counter, sizeof(_local_sign_counter), 1, _db_file);
91-
92-
fseek(_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
93-
/* we read the entries partially and fill the offsets ourselves*/
94-
for (size_t i = 0; i < get_entry_count(); i++) {
95-
fread(&_entries[i], DB_SIZE_ENTRY, 1, _db_file);
81+
bool restore_toggle;
82+
if (fread(&restore_toggle, sizeof(bool), 1, _db_file) == 1) {
83+
if (restore_toggle) {
84+
restore();
9685
}
9786
}
9887

@@ -107,49 +96,55 @@ FileSecurityDb::~FileSecurityDb() {
10796
}
10897

10998
FILE* FileSecurityDb::open_db_file(const char *db_path) {
99+
if (!db_path) {
100+
return NULL;
101+
}
102+
110103
FILE *db_file = fopen(db_path, "wb+");
111-
if (db_file) {
112-
/* we will check the db file and if the version or size doesn't match
113-
* what we expect we will blank it */
114-
bool init = false;
115-
uint16_t version;
116-
117-
fseek(db_file, DB_OFFSET_VERSION, SEEK_SET);
118-
119-
if ((fread(&version, sizeof(version), 1, db_file) == 1) &&
120-
(version == DB_VERSION)) {
121-
/* version checks out, try the size */
122-
fseek(db_file, DB_SIZE - 1, SEEK_SET);
123-
/* read one byte and expect to hit EOF */
124-
if ((fread(&version, 1, 1, db_file) != 1) || !feof(db_file)) {
125-
init = true;
126-
}
127-
} else {
104+
105+
if (!db_file) {
106+
return NULL;
107+
}
108+
109+
/* we will check the db file and if the version or size doesn't match
110+
* what we expect we will blank it */
111+
bool init = false;
112+
uint16_t version;
113+
114+
fseek(db_file, DB_OFFSET_VERSION, SEEK_SET);
115+
116+
if ((fread(&version, sizeof(version), 1, db_file) == 1) &&
117+
(version == DB_VERSION)) {
118+
/* version checks out, try the size */
119+
fseek(db_file, DB_SIZE - 1, SEEK_SET);
120+
/* read one byte and expect to hit EOF */
121+
if ((fread(&version, 1, 1, db_file) != 1) || !feof(db_file)) {
128122
init = true;
129123
}
124+
} else {
125+
init = true;
126+
}
130127

131-
if (init) {
132-
fseek(db_file, 0, SEEK_SET);
133-
134-
/* zero the file */
135-
const uint32_t zero = 0;
136-
size_t count = DB_SIZE / 4;
137-
while (count--) {
138-
if (fwrite(&zero, sizeof(zero), 1, db_file) != 1) {
139-
fclose(db_file);
140-
return NULL;
141-
}
142-
}
128+
if (init) {
129+
fseek(db_file, 0, SEEK_SET);
143130

144-
if (fflush(db_file)) {
131+
/* zero the file */
132+
const uint32_t zero = 0;
133+
size_t count = DB_SIZE / 4;
134+
while (count--) {
135+
if (fwrite(&zero, sizeof(zero), 1, db_file) != 1) {
145136
fclose(db_file);
146137
return NULL;
147138
}
148139
}
149140

150-
return db_file;
141+
if (fflush(db_file)) {
142+
fclose(db_file);
143+
return NULL;
144+
}
151145
}
152-
return NULL;
146+
147+
return db_file;
153148
}
154149

155150
SecurityDistributionFlags_t* FileSecurityDb::get_distribution_flags(
@@ -287,6 +282,21 @@ void FileSecurityDb::set_entry_peer_sign_counter(
287282
/* saving and loading from nvm */
288283

289284
void FileSecurityDb::restore() {
285+
fseek(_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
286+
fread(&_local_identity, sizeof(_local_identity), 1, _db_file);
287+
288+
fseek(_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
289+
fread(&_local_csrk, sizeof(_local_csrk), 1, _db_file);
290+
291+
fseek(_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
292+
fread(&_local_sign_counter, sizeof(_local_sign_counter), 1, _db_file);
293+
294+
fseek(_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
295+
/* we read the entries partially and fill the offsets ourselves*/
296+
for (size_t i = 0; i < get_entry_count(); i++) {
297+
fread(&_entries[i], DB_SIZE_ENTRY, 1, _db_file);
298+
}
299+
290300
}
291301

292302
void FileSecurityDb::sync(entry_handle_t db_handle) {
@@ -300,8 +310,12 @@ void FileSecurityDb::sync(entry_handle_t db_handle) {
300310
}
301311

302312
void FileSecurityDb::set_restore(bool reload) {
313+
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
314+
fwrite(&reload, sizeof(bool), 1, _db_file);
303315
}
304316

317+
/* helper functions */
318+
305319
uint8_t FileSecurityDb::get_entry_count() {
306320
return MAX_ENTRIES;
307321
}

0 commit comments

Comments
 (0)