Skip to content

Commit

Permalink
[android] Peristent storage fixes (#6888)
Browse files Browse the repository at this point in the history
* [android] Peristent storage fixes

Fix issues in the code for storing persistent data, causing
crashes at the beginning of commissioning process in Android
CHIP Tool.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
Damian-Nordic and restyled-commits authored May 18, 2021
1 parent fab7747 commit 403b5ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/controller/ExampleOperationalCredentialsIssuer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ CHIP_ERROR ExampleOperationalCredentialsIssuer::Initialize(PersistentStorageDele
// Storage doesn't have an existing keypair. Let's create one and add it to the storage.
ReturnErrorOnFailure(mIssuer.Initialize());
ReturnErrorOnFailure(mIssuer.Serialize(serializedKey));

keySize = static_cast<uint16_t>(sizeof(serializedKey));
ReturnErrorOnFailure(storage.SyncSetKeyValue(kOperationalCredentialsIssuerKeypairStorage, &serializedKey, keySize));
}
else
Expand Down
22 changes: 12 additions & 10 deletions src/controller/java/AndroidKeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <platform/KeyValueStoreManager.h>

#include <algorithm>
#include <memory>
#include <string.h>

#include "CHIPJNIError.h"
Expand All @@ -32,7 +33,7 @@ namespace DeviceLayer {
namespace PersistedStorage {
namespace {

constexpr size_t kMaxKvsValueBytes = 1024;
constexpr size_t kMaxKvsValueBytes = 4096;
constexpr size_t kMaxKvsValueEncodedChars = BASE64_ENCODED_LEN(kMaxKvsValueBytes) + 1;

} // namespace
Expand Down Expand Up @@ -65,14 +66,16 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t
}

JniUtfString utfValue(env, (jstring) javaValue);
if (strlen(utfValue.c_str()) > kMaxKvsValueEncodedChars)
const size_t utfValueLen = strlen(utfValue.c_str());

if (utfValueLen > kMaxKvsValueEncodedChars)
{
ChipLogError(DeviceLayer, "Unexpected large value received from KVS");
return CHIP_ERROR_NO_MEMORY;
}

uint8_t buffer[kMaxKvsValueBytes];
uint16_t decodedLength = chip::Base64Decode(utfValue.c_str(), strlen(utfValue.c_str()), buffer);
std::unique_ptr<uint8_t[]> buffer(new uint8_t[BASE64_MAX_DECODED_LEN(utfValueLen)]);
uint16_t decodedLength = chip::Base64Decode(utfValue.c_str(), utfValueLen, buffer.get());
if (decodedLength == UINT16_MAX)
{
ChipLogError(DeviceLayer, "KVS base64 decoding failed");
Expand All @@ -86,7 +89,7 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t

if (value != nullptr)
{
memcpy(value, buffer, std::min<size_t>(value_size, decodedLength));
memcpy(value, buffer.get(), std::min<size_t>(value_size, decodedLength));
}

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -124,14 +127,13 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value,
JNIEnv * env = GetEnvForCurrentThread();
ReturnErrorCodeIf(env == nullptr, CHIP_ERROR_INTERNAL);

char base64Buffer[kMaxKvsValueEncodedChars];

size_t length = chip::Base64Encode(static_cast<const uint8_t *>(value), value_size, base64Buffer);
std::unique_ptr<char[]> buffer(new char[BASE64_ENCODED_LEN(value_size) + 1]);

base64Buffer[length] = 0;
size_t length = chip::Base64Encode(static_cast<const uint8_t *>(value), value_size, buffer.get());
buffer.get()[length] = 0;

UtfString utfKey(env, key);
UtfString utfBase64Value(env, base64Buffer);
UtfString utfBase64Value(env, buffer.get());

env->CallStaticVoidMethod(mKeyValueStoreManagerClass, mSetMethod, utfKey.jniValue(), utfBase64Value.jniValue());

Expand Down
2 changes: 1 addition & 1 deletion src/transport/AdminPairingTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ CHIP_ERROR AdminPairingInfo::StoreIntoKVS(PersistentStorageDelegate * kvs)
err = kvs->SyncSetKeyValue(key, info, sizeof(StorableAdminPairingInfo));
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Error occurred calling SyncSetKeyValue.");
ChipLogError(Discovery, "Error occurred calling SyncSetKeyValue: %s", chip::ErrorStr(err));
}

exit:
Expand Down

0 comments on commit 403b5ff

Please sign in to comment.