Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tizen] Account for 0-terminator in WriteConfigValueStr #26510

Merged
merged 3 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/platform/Tizen/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
arkq marked this conversation as resolved.
Show resolved Hide resolved

return CHIP_NO_ERROR;
}

CHIP_ERROR PosixConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/platform/tests/TestConfigurationMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
Expand Down