From 6296b8d769f42ae70cf613fa6a40e920a5e6283e Mon Sep 17 00:00:00 2001 From: ni-sujain <98735487+ni-sujain@users.noreply.github.com> Date: Wed, 13 Mar 2024 15:19:25 +0530 Subject: [PATCH] fixing crashes related to Repeated NestedMessage Parsing and bugs in PackToAny API (#352) --- src/lv_message_efficient.cc | 22 +++++++++++----------- src/lv_message_efficient.h | 1 + src/lv_message_value.cc | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/lv_message_efficient.cc b/src/lv_message_efficient.cc index 1a058c4e..7ed4d624 100644 --- a/src/lv_message_efficient.cc +++ b/src/lv_message_efficient.cc @@ -68,16 +68,18 @@ namespace grpc_labview //--------------------------------------------------------------------- //--------------------------------------------------------------------- const char *LVMessageEfficient::ParseString(google::protobuf::uint32 tag, const MessageElementMetadata& fieldInfo, uint32_t index, const char *protobuf_ptr, ParseContext *ctx) - { + { const char* lv_ptr = (this->GetLVClusterHandleSharedPtr()) + fieldInfo.clusterOffset; if (fieldInfo.isRepeated) - { + { + std::string key = fieldInfo.fieldName + std::to_string(_currentIndexForRepeatedMessageValue); + // Get the _repeatedMessageValues vector from the map - auto _repeatedStringValuesIt = _repeatedStringValuesMap.find(fieldInfo.fieldName); + auto _repeatedStringValuesIt = _repeatedStringValuesMap.find(key); if (_repeatedStringValuesIt == _repeatedStringValuesMap.end()) { - _repeatedStringValuesIt = _repeatedStringValuesMap.emplace(fieldInfo.fieldName, google::protobuf::RepeatedField()).first; + _repeatedStringValuesIt = _repeatedStringValuesMap.emplace(key, google::protobuf::RepeatedField()).first; } protobuf_ptr -= 1; @@ -91,12 +93,11 @@ namespace grpc_labview } } while (ExpectTag(tag, protobuf_ptr)); - auto arraySize = sizeof(void*) * _repeatedStringValuesIt->second.size(); - auto _lvProvidedArrayHandle = *(void**)lv_ptr; - *(void**)lv_ptr = DSNewHandle(arraySize); + + NumericArrayResize(0x08, 1, reinterpret_cast(const_cast(lv_ptr)), _repeatedStringValuesIt->second.size()); auto arrayHandle = *(LV1DArrayHandle*)lv_ptr; (*arrayHandle)->cnt = _repeatedStringValuesIt->second.size(); - + // Copy the repeated string values into the LabVIEW array auto lvStringPtr = (*arrayHandle)->bytes(); for (auto str:_repeatedStringValuesIt->second) @@ -178,6 +179,7 @@ namespace grpc_labview auto _vectorPtr = _repeatedMessageValuesIt->second.get()->_buffer.data(); _vectorPtr = _vectorPtr + (elementIndex * clusterSize); nestedMessage.SetLVClusterHandle(_vectorPtr); + nestedMessage._currentIndexForRepeatedMessageValue = elementIndex; protobuf_ptr = ctx->ParseMessage(&nestedMessage, protobuf_ptr); elementIndex++; @@ -214,9 +216,7 @@ namespace grpc_labview // shrink the array to the correct size auto arraySize = numElements * clusterSize; - auto old_arrayHandle = *(void**)lv_ptr; - DSDisposeHandle(old_arrayHandle); - *(void**)lv_ptr = DSNewHandle(arraySize); + NumericArrayResize(0x08, 1, reinterpret_cast(const_cast(lv_ptr)), arraySize); auto arrayHandle = *(LV1DArrayHandle*)lv_ptr; (*arrayHandle)->cnt = numElements; diff --git a/src/lv_message_efficient.h b/src/lv_message_efficient.h index dab6b718..3bc59347 100644 --- a/src/lv_message_efficient.h +++ b/src/lv_message_efficient.h @@ -38,6 +38,7 @@ namespace grpc_labview std::unordered_map _repeatedField_continueIndex; std::unordered_map> _repeatedMessageValuesMap; std::unordered_map> _repeatedStringValuesMap; + uint64_t _currentIndexForRepeatedMessageValue = 0; protected: const char *ParseBoolean(const MessageElementMetadata& fieldInfo, uint32_t index, const char *ptr, google::protobuf::internal::ParseContext *ctx); diff --git a/src/lv_message_value.cc b/src/lv_message_value.cc index 74927e6a..d602d99d 100644 --- a/src/lv_message_value.cc +++ b/src/lv_message_value.cc @@ -52,7 +52,7 @@ namespace grpc_labview size_t LVRepeatedNestedMessageMessageValue::ByteSizeLong() { size_t totalSize = 0; - totalSize += 1UL * _value.size(); + totalSize += WireFormatLite::TagSize(_protobufId, WireFormatLite::TYPE_MESSAGE) * _value.size(); for (const auto& msg : _value) { totalSize += WireFormatLite::MessageSize(*msg);