More complete error handling in SerializeProto

PiperOrigin-RevId: 611073556
Change-Id: Id29c3dbe15bb55c7d4e68d9a3e1c82327af0ad23
This commit is contained in:
Wiktor Garbacz 2024-02-28 05:46:27 -08:00 committed by Copybara-Service
parent 2430bc8ae8
commit f7f4cdb458

View File

@ -17,6 +17,7 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "absl/status/status.h" #include "absl/status/status.h"
@ -47,7 +48,11 @@ absl::StatusOr<std::vector<uint8_t>> SerializeProto(
// Wrap protobuf in a envelope so that we know the name of the protobuf // Wrap protobuf in a envelope so that we know the name of the protobuf
// structure when deserializing in the sandboxee. // structure when deserializing in the sandboxee.
ProtoArg proto_arg; ProtoArg proto_arg;
proto_arg.set_protobuf_data(proto.SerializeAsString()); std::string proto_data;
if (!proto.SerializeToString(&proto_data)) {
return absl::InternalError("Unable to serialize proto data");
}
proto_arg.set_protobuf_data(std::move(proto_data));
proto_arg.set_full_name(proto.GetTypeName()); proto_arg.set_full_name(proto.GetTypeName());
std::vector<uint8_t> serialized_proto(proto_arg.ByteSizeLong()); std::vector<uint8_t> serialized_proto(proto_arg.ByteSizeLong());
if (!proto_arg.SerializeToArray(serialized_proto.data(), if (!proto_arg.SerializeToArray(serialized_proto.data(),