Skip to content

Commit

Permalink
Fix PersistentStorageDelegate 0-storage behavior
Browse files Browse the repository at this point in the history
- Make Server's PersistentStorageDelegate and
  TestPersistentStorage delegate properly handle nullptr
  input for zero-length.
- Document behavior expected of PersistentStorageDelegate interface

Issue #16130
  • Loading branch information
tcarmelveilleux committed Mar 11, 2022
1 parent 86a8444 commit fe14524
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ class Server

CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override
{
uint8_t placeholderForEmpty = 0;
if (value == nullptr)
{
if (size == 0)
{
value = &placeholderForEmpty;
}
else
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
}
return DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(key, value, size);
}

Expand Down
10 changes: 7 additions & 3 deletions src/lib/core/CHIPPersistentStorageDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ class DLL_EXPORT PersistentStorageDelegate

/**
* @brief
* Set the value for the key to a byte buffer.
* Set the value for the key to a byte buffer. Empty keys can be stored
* with size == 0.
*
* @param[in] key Key to be set
* @param[in] value Value to be set
* @param[in] size Size of the Value
* @param[in] value Pointer to buytes of value to be set. `value` can only be `nullptr` if size == 0.
* @param[in] size Size of the `value` to store.
*
* @return CHIP_NO_ERROR on success, CHIP_INVALID_ARGUMENT on bad assumptions, other
* CHIP_ERROR value from implementation on failure.
*/
virtual CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) = 0;

Expand Down
22 changes: 20 additions & 2 deletions src/lib/support/TestPersistentStorageDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,27 @@ class TestPersistentStorageDelegate : public PersistentStorageDelegate
{
return CHIP_ERROR_PERSISTED_STORAGE_FAILED;
}

const uint8_t * bytes = static_cast<const uint8_t *>(value);
mStorage[key] = std::vector<uint8_t>(bytes, bytes + size);
return CHIP_NO_ERROR;

// Handle empty values
if (value == nullptr)
{
if (size == 0)
{
mStorage[key] = std::vector<uint8_t>();
return CHIP_NO_ERROR;
}
else
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
}
else
{
mStorage[key] = std::vector<uint8_t>(bytes, bytes + size);
return CHIP_NO_ERROR;
}
}

CHIP_ERROR SyncDeleteKeyValue(const char * key) override
Expand Down

0 comments on commit fe14524

Please sign in to comment.