Skip to content

Commit ef79ae9

Browse files
committed
Validate NVS storage namespace
1 parent b57e75b commit ef79ae9

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

components/ocs_storage/nvs_storage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ const char* log_tag = "nvs_storage";
2424
} // namespace
2525

2626
NvsStorage::NvsStorage(const char* ns) {
27+
configASSERT(strlen(ns) <= max_namespace_len);
28+
2729
memset(ns_, 0, sizeof(ns_));
28-
strncpy(ns_, ns, std::min(bufsize_, strlen(ns)));
30+
strncpy(ns_, ns, std::min(max_namespace_len, strlen(ns)));
2931
}
3032

3133
status::StatusCode NvsStorage::probe(const char* key, size_t& size) {

components/ocs_storage/nvs_storage.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ namespace storage {
2121

2222
class NvsStorage : public IStorage, public core::NonCopyable<> {
2323
public:
24+
//! Maximum number of symbols for the storage namespace, without NULL character.
25+
static constexpr unsigned max_namespace_len = NVS_KEY_NAME_MAX_SIZE - 1;
26+
2427
//! Initialize.
2528
//!
2629
//! @params
2730
//! - @p ns - NVS namespace.
2831
//!
2932
//! @remarks
30-
//! NVS should be initialized.
33+
//! - NVS should be initialized.
34+
//! - namespace length should not exceed @p max_namespace_len.
3135
explicit NvsStorage(const char* ns);
3236

3337
//! Read data size from the configured namespace.
@@ -53,9 +57,7 @@ class NvsStorage : public IStorage, public core::NonCopyable<> {
5357

5458
status::StatusCode erase_(nvs_handle_t handle, const char* key);
5559

56-
static const constexpr unsigned bufsize_ = NVS_KEY_NAME_MAX_SIZE - 1;
57-
58-
char ns_[bufsize_ + 1];
60+
char ns_[max_namespace_len + 1];
5961
};
6062

6163
} // namespace storage

0 commit comments

Comments
 (0)