Skip to content

Commit

Permalink
fixing crashes related to Repeated NestedMessage Parsing and bugs in …
Browse files Browse the repository at this point in the history
…PackToAny API (#352)
  • Loading branch information
ni-sujain authored Mar 13, 2024
1 parent 8454924 commit 6296b8d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/lv_message_efficient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>()).first;
_repeatedStringValuesIt = _repeatedStringValuesMap.emplace(key, google::protobuf::RepeatedField<std::string>()).first;
}

protobuf_ptr -= 1;
Expand All @@ -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<void*>(const_cast<char*>(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<LStrHandle>();
for (auto str:_repeatedStringValuesIt->second)
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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<void*>(const_cast<char*>(lv_ptr)), arraySize);
auto arrayHandle = *(LV1DArrayHandle*)lv_ptr;
(*arrayHandle)->cnt = numElements;

Expand Down
1 change: 1 addition & 0 deletions src/lv_message_efficient.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace grpc_labview
std::unordered_map<std::string, uint32_t> _repeatedField_continueIndex;
std::unordered_map<std::string, std::shared_ptr<RepeatedMessageValue>> _repeatedMessageValuesMap;
std::unordered_map<std::string, google::protobuf::RepeatedField<std::string>> _repeatedStringValuesMap;
uint64_t _currentIndexForRepeatedMessageValue = 0;

protected:
const char *ParseBoolean(const MessageElementMetadata& fieldInfo, uint32_t index, const char *ptr, google::protobuf::internal::ParseContext *ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/lv_message_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6296b8d

Please sign in to comment.