From 27ca0452dd86d6b777e24ad90b9939e79ce2e9e8 Mon Sep 17 00:00:00 2001 From: pankore <86098180+pankore@users.noreply.github.com> Date: Wed, 24 Aug 2022 00:08:00 +0800 Subject: [PATCH] [KVS] Check NULL pointer for getPref_bin_new (#21854) * [KVS] Add NULL pointer check for getPref_bin_new Invalid read_bytes_size pointer could cause hard fault on getPref_bin_new function. * [KVS] Pass in dummy_read_bytes when read_bytes is nullptr * [Restyle] Fix styling * [KVS] Add nullptr check after pvPortMalloc * [Restyle] Fix style Co-authored-by: Andrei Litvin --- src/platform/Ameba/KeyValueStoreManagerImpl.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/platform/Ameba/KeyValueStoreManagerImpl.cpp b/src/platform/Ameba/KeyValueStoreManagerImpl.cpp index 74e8a2882f6cb7..93ec38b8611b58 100644 --- a/src/platform/Ameba/KeyValueStoreManagerImpl.cpp +++ b/src/platform/Ameba/KeyValueStoreManagerImpl.cpp @@ -49,7 +49,20 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t return (err = CHIP_ERROR_NOT_IMPLEMENTED); } - ret = getPref_bin_new(key, key, (uint8_t *) value, value_size, read_bytes_size); + if (read_bytes_size) + { + ret = getPref_bin_new(key, key, (uint8_t *) value, value_size, read_bytes_size); + } + else + { + size_t * dummy_read_bytes_size = (size_t *) pvPortMalloc(sizeof(size_t)); + if (!dummy_read_bytes_size) + { + return CHIP_ERROR_INTERNAL; + } + ret = getPref_bin_new(key, key, (uint8_t *) value, value_size, dummy_read_bytes_size); + vPortFree(dummy_read_bytes_size); + } switch (ret) { case 0: