diff --git a/camera/metadata/include/metadata_log.h b/camera/metadata/include/metadata_log.h index 3d046950fa9f857563ea8c23f5963e5616d0e06e..2dcbd9fcaf0d4ef7ef635d4bcde58470097eccc5 100644 --- a/camera/metadata/include/metadata_log.h +++ b/camera/metadata/include/metadata_log.h @@ -56,4 +56,12 @@ #define METADATA_ERR (-3) #define METADATA_PERMISSION_DENIED (-4) +#define METADATA_CHECK_ERROR_RETURN_RET_LOG(cond, ret, fmt, ...) \ + do { \ + if (cond) { \ + METADATA_ERR_LOG(fmt, ##__VA_ARGS__); \ + return ret; \ + } \ + } while (0) + #endif // OHOS_CAMERA_LOG_H diff --git a/camera/metadata/src/metadata_utils.cpp b/camera/metadata/src/metadata_utils.cpp index f7de85d759893868924b13d7a83be069c8ae58ca..521fc0d760f6a982e83301fbadd67b538ae18091 100644 --- a/camera/metadata/src/metadata_utils.cpp +++ b/camera/metadata/src/metadata_utils.cpp @@ -411,13 +411,13 @@ std::string MetadataUtils::EncodeToString(std::shared_ptr metada camera_metadata_item_entry_t *item = GetMetadataItems(meta); for (uint32_t index = 0; index < meta->item_count; index++, item++) { ret = memcpy_s(encodeData, encodeDataLen, item, itemFixedLen); - if (ret != EOK) { - METADATA_ERR_LOG("MetadataUtils::EncodeToString Failed to copy memory for item fixed fields"); - return {}; - } + METADATA_CHECK_ERROR_RETURN_RET_LOG( + ret != EOK, {}, "MetadataUtils::EncodeToString Failed to copy memory for item fixed fields"); encodeData += itemFixedLen; encodeDataLen -= itemFixedLen; int32_t dataLen = itemLen - itemFixedLen; + METADATA_CHECK_ERROR_RETURN_RET_LOG( + item == nullptr, {}, "MetadataUtils::EncodeToString Failed, item is nullptr"); ret = memcpy_s(encodeData, encodeDataLen, &(item->data), dataLen); if (ret != EOK) { METADATA_ERR_LOG("MetadataUtils::EncodeToString Failed to copy memory for item data field"); @@ -494,16 +494,16 @@ std::shared_ptr MetadataUtils::DecodeFromString(std::string sett decodeData += headerLength; camera_metadata_item_entry_t *item = GetMetadataItems(meta); for (uint32_t index = 0; index < meta->item_count; index++, item++) { - if (totalLen < ((decodeData - &setting[0]) + itemLen)) { - METADATA_ERR_LOG("MetadataUtils::DecodeFromString Failed at item index: %{public}u", index); - return {}; - } + METADATA_CHECK_ERROR_RETURN_RET_LOG(totalLen < ((decodeData - &setting[0]) + itemLen), {}, + "MetadataUtils::DecodeFromString Failed at item index: %{public}u", index); ret = memcpy_s(item, itemFixedLen, decodeData, itemFixedLen); IF_COND_PRINT_MSG_AND_RETURN(ret != EOK, "MetadataUtils::DecodeFromString Failed to copy memory for item fixed fields") decodeData += itemFixedLen; uint32_t dataLen = itemLen - itemFixedLen; + METADATA_CHECK_ERROR_RETURN_RET_LOG( + item == nullptr, {}, "MetadataUtils::DecodeFromString Failed, item is nullptr"); ret = memcpy_s(&(item->data), dataLen, decodeData, dataLen); if (item->data_type >= META_NUM_TYPES || totalLen < (uint64_t)(item->count * OHOS_CAMERA_METADATA_TYPE_SIZE[item->data_type])) {