From 916010e8dff0fb0a7ac46380b5659aba1b609308 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Thu, 11 May 2023 14:52:14 +0200 Subject: [PATCH 1/3] [Tizen] Account for 0-terminator in WriteConfigValueStr --- src/platform/Tizen/PosixConfig.cpp | 20 +++++++++++++++++--- src/platform/tests/TestConfigurationMgr.cpp | 5 ++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/platform/Tizen/PosixConfig.cpp b/src/platform/Tizen/PosixConfig.cpp index fc65c24bf189a9..994dd9dffd2520 100644 --- a/src/platform/Tizen/PosixConfig.cpp +++ b/src/platform/Tizen/PosixConfig.cpp @@ -101,7 +101,14 @@ CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint64_t & val) CHIP_ERROR PosixConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) { VerifyOrReturnError(buf != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - return PersistedStorage::KeyValueStoreMgr().Get(key.Name, buf, bufSize, &outLen); + + auto err = PersistedStorage::KeyValueStoreMgr().Get(key.Name, buf, bufSize, &outLen); + VerifyOrReturnError(err == CHIP_NO_ERROR, err); + + VerifyOrReturnError(outLen > 0, CHIP_ERROR_PERSISTED_STORAGE_FAILED); + outLen--; // Account for null terminator + + return CHIP_NO_ERROR; } CHIP_ERROR PosixConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) @@ -133,13 +140,20 @@ CHIP_ERROR PosixConfig::WriteConfigValue(Key key, uint64_t val) CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str) { VerifyOrReturnError(str != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - return PersistedStorage::KeyValueStoreMgr().Put(key.Name, str, strlen(str)); + return PersistedStorage::KeyValueStoreMgr().Put(key.Name, str, strlen(str) + 1); } CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen) { VerifyOrReturnError(str != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - return PersistedStorage::KeyValueStoreMgr().Put(key.Name, str, strLen); + + auto * strCopy = strndup(str, strLen); + VerifyOrReturnError(strCopy != nullptr, CHIP_ERROR_NO_MEMORY); + + auto err = PersistedStorage::KeyValueStoreMgr().Put(key.Name, strCopy, strLen + 1); + + free(strCopy); + return err; } CHIP_ERROR PosixConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) diff --git a/src/platform/tests/TestConfigurationMgr.cpp b/src/platform/tests/TestConfigurationMgr.cpp index dafe5b209725a2..a113594868ad2c 100644 --- a/src/platform/tests/TestConfigurationMgr.cpp +++ b/src/platform/tests/TestConfigurationMgr.cpp @@ -449,7 +449,6 @@ static void TestConfigurationMgr_GetProductId(nlTestSuite * inSuite, void * inCo * Test Suite. It lists all the test functions. */ static const nlTest sTests[] = { - NL_TEST_DEF("Test PlatformMgr::Init", TestPlatformMgr_Init), #if !defined(NDEBUG) NL_TEST_DEF("Test PlatformMgr::RunUnitTest", TestPlatformMgr_RunUnitTest), @@ -466,8 +465,8 @@ static const nlTest sTests[] = { NL_TEST_DEF("Test ConfigurationMgr::GetVendorId", TestConfigurationMgr_GetVendorId), NL_TEST_DEF("Test ConfigurationMgr::GetProductName", TestConfigurationMgr_GetProductName), NL_TEST_DEF("Test ConfigurationMgr::GetProductId", TestConfigurationMgr_GetProductId), - NL_TEST_SENTINEL() -}; // namespace + NL_TEST_SENTINEL(), +}; /** * Set up the test suite. From 65e1eec30dd0c3a24a086143a1ce0f1e213b742a Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 12 May 2023 09:09:08 +0200 Subject: [PATCH 2/3] Fix null-terminator implementation after review --- src/platform/Tizen/PosixConfig.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/platform/Tizen/PosixConfig.cpp b/src/platform/Tizen/PosixConfig.cpp index 994dd9dffd2520..589efef965adb2 100644 --- a/src/platform/Tizen/PosixConfig.cpp +++ b/src/platform/Tizen/PosixConfig.cpp @@ -105,8 +105,10 @@ CHIP_ERROR PosixConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, auto err = PersistedStorage::KeyValueStoreMgr().Get(key.Name, buf, bufSize, &outLen); VerifyOrReturnError(err == CHIP_NO_ERROR, err); - VerifyOrReturnError(outLen > 0, CHIP_ERROR_PERSISTED_STORAGE_FAILED); - outLen--; // Account for null terminator + // We are storing string values in the config store without + // the null terminator, so we need to add it here. + VerifyOrReturnError(bufSize >= outLen + 1, CHIP_ERROR_NO_MEMORY); + buf[outLen] = '\0'; return CHIP_NO_ERROR; } @@ -140,20 +142,13 @@ CHIP_ERROR PosixConfig::WriteConfigValue(Key key, uint64_t val) CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str) { VerifyOrReturnError(str != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - return PersistedStorage::KeyValueStoreMgr().Put(key.Name, str, strlen(str) + 1); + return PersistedStorage::KeyValueStoreMgr().Put(key.Name, str, strlen(str)); } CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen) { VerifyOrReturnError(str != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - - auto * strCopy = strndup(str, strLen); - VerifyOrReturnError(strCopy != nullptr, CHIP_ERROR_NO_MEMORY); - - auto err = PersistedStorage::KeyValueStoreMgr().Put(key.Name, strCopy, strLen + 1); - - free(strCopy); - return err; + return PersistedStorage::KeyValueStoreMgr().Put(key.Name, str, strLen); } CHIP_ERROR PosixConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) From e7a499937b574e3f1be6498fc7f15dd29ee8aaf1 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 12 May 2023 21:02:21 +0200 Subject: [PATCH 3/3] Update src/platform/Tizen/PosixConfig.cpp Co-authored-by: Boris Zbarsky --- src/platform/Tizen/PosixConfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/Tizen/PosixConfig.cpp b/src/platform/Tizen/PosixConfig.cpp index 589efef965adb2..c8b2d36d8271bf 100644 --- a/src/platform/Tizen/PosixConfig.cpp +++ b/src/platform/Tizen/PosixConfig.cpp @@ -107,7 +107,7 @@ CHIP_ERROR PosixConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, // We are storing string values in the config store without // the null terminator, so we need to add it here. - VerifyOrReturnError(bufSize >= outLen + 1, CHIP_ERROR_NO_MEMORY); + VerifyOrReturnError(bufSize >= outLen + 1, CHIP_ERROR_BUFFER_TOO_SMALL); buf[outLen] = '\0'; return CHIP_NO_ERROR;