-
Couldn't load subscription status.
- Fork 15.9k
Description
What version of protobuf and what language are you using?
v30.2
Language: C++
What operating system (Linux, Windows, ...) and version?
Linux 5.10.238
What runtime / compiler are you using (e.g., python version or gcc version)
gcc (GCC) 10.2.1 20210130 (Red Hat 10.2.1-11)
What did you do?
Steps to reproduce the behavior:
The following program:
#include <signal.h>
#include <unistd.h>
#include <google/protobuf/util/delimited_message_util.h>
#include <google/protobuf/type.pb.h>
int main(int argc, char** argv) {
::signal(SIGPIPE, SIG_IGN); // Ignore pipe signals.
int fd[2];
::pipe(fd);
::close(fd[0]); // Close read-end of pipe.
google::protobuf::Type message;
bool r = google::protobuf::util::SerializeDelimitedToFileDescriptor(
message, fd[1]);
std::cout << (r ? "true" : "false") << std::endl;
return 0;
}
attempts to serialize to a broken pipe using google::protobuf::util::SerializeDelimitedToFileDescriptor and emits a line to the console indicating whether the method returned true or false.
What did you expect to see
Expect to see false as the pipe being written to is broken.
What did you see instead?
true indicating the error writing to the pipe was absorbed.
The issue is the implementation of google::protobuf::util::SerializeDelimitedToFileDescriptor:
bool SerializeDelimitedToFileDescriptor(const MessageLite& message,
int file_descriptor) {
io::FileOutputStream output(file_descriptor);
return SerializeDelimitedToZeroCopyStream(message, &output);
}
only checks that message can be serialized to the internal FileOutputStream and does not check that this stream is successfully flushed to the given file descriptor at destruction. Currently, the FileOutputStream is only flushed in its destructor giving no opportunity for an error to be propagated.
The fix is to explicitly perform the flush in the return statement:
return SerializeDelimitedToZeroCopyStream(message, &output) && output.Flush();
Anything else we should know about your project / environment
I can push a fix if desired.