Skip to content

Commit

Permalink
[Cord] Backport-1.58: Fix MakeCordFromSlice memory bug (grpc#34553)
Browse files Browse the repository at this point in the history
Backport of grpc#34549
  • Loading branch information
veblush authored Oct 6, 2023
1 parent ac5c3f3 commit ce4257d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions include/grpcpp/support/proto_buffer_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,17 @@ class ProtoBufferReader : public grpc::protobuf::io::ZeroCopyInputStream {
// This function takes ownership of slice and return a newly created Cord off
// of it.
static absl::Cord MakeCordFromSlice(grpc_slice slice) {
// slice_for_cord is created to keep inlined data of the given slice
grpc_slice* slice_for_cord = new grpc_slice;
*slice_for_cord = slice;
return absl::MakeCordFromExternal(
absl::string_view(reinterpret_cast<char*>(GRPC_SLICE_START_PTR(slice)),
GRPC_SLICE_LENGTH(slice)),
[slice](absl::string_view /* view */) { grpc_slice_unref(slice); });
absl::string_view(
reinterpret_cast<char*>(GRPC_SLICE_START_PTR(*slice_for_cord)),
GRPC_SLICE_LENGTH(*slice_for_cord)),
[slice_for_cord](absl::string_view /* view */) {
grpc_slice_unref(*slice_for_cord);
delete slice_for_cord;
});
}
#endif // GRPC_PROTOBUF_CORD_SUPPORT_ENABLED

Expand Down

0 comments on commit ce4257d

Please sign in to comment.