components/nvs: strlcpy is not available on Linux, replace with strncpy and terminate strings explicitly

This commit is contained in:
Ivan Grokhotkov 2016-08-23 15:14:13 +08:00
parent 3df4130eb7
commit 9ef827ae20
5 changed files with 12 additions and 10 deletions

View file

@ -18,7 +18,7 @@
#include "crc.h" #include "crc.h"
#endif #endif
#include <cstdio> #include <cstdio>
#include <cstring>
namespace nvs namespace nvs
{ {
@ -156,8 +156,8 @@ esp_err_t Page::writeItem(uint8_t nsIndex, ItemType datatype, const char* key, c
std::fill_n(reinterpret_cast<uint32_t*>(item.key), sizeof(item.key) / 4, 0xffffffff); std::fill_n(reinterpret_cast<uint32_t*>(item.key), sizeof(item.key) / 4, 0xffffffff);
std::fill_n(reinterpret_cast<uint32_t*>(item.data), sizeof(item.data) / 4, 0xffffffff); std::fill_n(reinterpret_cast<uint32_t*>(item.data), sizeof(item.data) / 4, 0xffffffff);
strlcpy(item.key, key, Item::MAX_KEY_LENGTH + 1); strncpy(item.key, key, sizeof(item.key) - 1);
item.key[sizeof(item.key) - 1] = 0;
if (datatype != ItemType::SZ && datatype != ItemType::BLOB) { if (datatype != ItemType::SZ && datatype != ItemType::BLOB) {
memcpy(item.data, data, dataSize); memcpy(item.data, data, dataSize);

View file

@ -161,7 +161,8 @@ esp_err_t Storage::createOrOpenNamespace(const char* nsName, bool canCreate, uin
NamespaceEntry* entry = new NamespaceEntry; NamespaceEntry* entry = new NamespaceEntry;
entry->mIndex = ns; entry->mIndex = ns;
strlcpy(entry->mName, nsName, sizeof(entry->mName)); strncpy(entry->mName, nsName, sizeof(entry->mName) - 1);
entry->mName[sizeof(entry->mName) - 1] = 0;
mNamespaces.push_back(entry); mNamespaces.push_back(entry);
} else { } else {
@ -250,4 +251,4 @@ void Storage::debugCheck()
} }
#endif //ESP_PLATFORM #endif //ESP_PLATFORM
} }

View file

@ -29,7 +29,7 @@ COVERAGE_FILES = $(OBJ_FILES:.o=.gc*)
$(OBJ_FILES): %.o: %.cpp $(OBJ_FILES): %.o: %.cpp
$(TEST_PROGRAM): $(OBJ_FILES) $(TEST_PROGRAM): $(OBJ_FILES)
gcc $(LDFLAGS) -o $(TEST_PROGRAM) $(OBJ_FILES) g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(OBJ_FILES)
$(OUTPUT_DIR): $(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR) mkdir -p $(OUTPUT_DIR)

View file

@ -19,7 +19,8 @@
struct TestNode : public intrusive_list_node<TestNode> { struct TestNode : public intrusive_list_node<TestNode> {
TestNode(const char* name_ = "", int num_ = 0) : num(num_) TestNode(const char* name_ = "", int num_ = 0) : num(num_)
{ {
strlcpy(name, name_, sizeof(name)); strncpy(name, name_, sizeof(name) - 1);
name[sizeof(name) - 1] = 0;
} }
char name[32]; char name[32];
int num; int num;

View file

@ -62,7 +62,7 @@ TEST_CASE("crc32 behaves as expected", "[nvs]")
CHECK(crc32_1 != item2.calculateCrc32()); CHECK(crc32_1 != item2.calculateCrc32());
item2 = item1; item2 = item1;
strlcpy(item2.key, "foo", Item::MAX_KEY_LENGTH); strncpy(item2.key, "foo", Item::MAX_KEY_LENGTH);
CHECK(crc32_1 != item2.calculateCrc32()); CHECK(crc32_1 != item2.calculateCrc32());
} }
@ -672,12 +672,12 @@ public:
} }
if (err == ESP_ERR_NVS_REMOVE_FAILED) { if (err == ESP_ERR_NVS_REMOVE_FAILED) {
written[index] = true; written[index] = true;
strlcpy(reinterpret_cast<char*>(values[index]), buf, strBufLen); strncpy(reinterpret_cast<char*>(values[index]), buf, strBufLen);
return ESP_ERR_FLASH_OP_FAIL; return ESP_ERR_FLASH_OP_FAIL;
} }
REQUIRE(err == ESP_OK); REQUIRE(err == ESP_OK);
written[index] = true; written[index] = true;
strlcpy(reinterpret_cast<char*>(values[index]), buf, strBufLen); strncpy(reinterpret_cast<char*>(values[index]), buf, strBufLen);
break; break;
} }