[Cord] Fix MakeCordFromSlice memory bug (#34549)

Fix `MakeCordFromSlice` function to fail to carry an inlined slice which
can give a corrupted result when deserializing a Cord.
pull/34554/head
Esun Kim 1 year ago committed by GitHub
parent a5f8585271
commit 4fa02accc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      include/grpcpp/support/proto_buffer_reader.h

@ -189,10 +189,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

Loading…
Cancel
Save