Merge pull request #21385 from hcaseyal/generic_unary

Generic API: assert when sending uninitialized byte buffer
pull/21427/head
hcaseyal 5 years ago committed by GitHub
commit 1917b1f794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      include/grpcpp/impl/codegen/byte_buffer.h
  2. 11
      include/grpcpp/impl/codegen/call_op_set.h

@ -63,13 +63,16 @@ class DeserializeFuncType;
class GrpcByteBufferPeer;
} // namespace internal
/// A sequence of bytes.
/// A sequence of bytes. A ByteBuffer must be backed by at least one slice to be
/// considered valid. You can consstruct an empty buffer through creating a
/// ByteBuffer backed by one empty slice.
class ByteBuffer final {
public:
/// Constuct an empty buffer.
/// Construct an uninitialized buffer.
ByteBuffer() : buffer_(nullptr) {}
/// Construct buffer from \a slices, of which there are \a nslices.
/// Construct buffer from \a slices, of which there are \a nslices. \a slices
/// must be a valid pointer.
ByteBuffer(const Slice* slices, size_t nslices) {
// The following assertions check that the representation of a grpc::Slice
// is identical to that of a grpc_slice: it has a grpc_slice field, and

@ -310,7 +310,12 @@ class CallOpSendMessage {
protected:
void AddOp(grpc_op* ops, size_t* nops) {
if (msg_ == nullptr && !send_buf_.Valid()) return;
if (msg_ == nullptr && !send_buf_.Valid()) {
// If SendMessage was called, this is an API misuse, since we're
// attempting to send an invalid message.
GPR_CODEGEN_DEBUG_ASSERT(!send_message_called_);
return;
}
if (hijacked_) {
serializer_ = nullptr;
return;
@ -329,6 +334,7 @@ class CallOpSendMessage {
}
void FinishOp(bool* status) {
if (msg_ == nullptr && !send_buf_.Valid()) return;
send_message_called_ = false;
if (hijacked_ && failed_send_) {
// Hijacking interceptor failed this Op
*status = false;
@ -369,6 +375,7 @@ class CallOpSendMessage {
const void* msg_ = nullptr; // The original non-serialized message
bool hijacked_ = false;
bool failed_send_ = false;
bool send_message_called_ = false;
ByteBuffer send_buf_;
WriteOptions write_options_;
std::function<Status(const void*)> serializer_;
@ -376,6 +383,8 @@ class CallOpSendMessage {
template <class M>
Status CallOpSendMessage::SendMessage(const M& message, WriteOptions options) {
GPR_CODEGEN_DEBUG_ASSERT(!send_message_called_);
send_message_called_ = true;
write_options_ = options;
serializer_ = [this](const void* message) {
bool own_buf;

Loading…
Cancel
Save