From ae055495cc874eac9e874be5e5dcf7d61d8a11d1 Mon Sep 17 00:00:00 2001 From: June Tate-Gans Date: Sun, 25 Apr 2021 14:29:40 -0500 Subject: [PATCH] g13d: Handle errors in writes The original code was just throwing away error conditions for write calls to an fd. Obviously this is bad, but really, we should probably be using an ostream instead of a raw file handle here. For now, deal with the warning by checking the result for errors, at least, and later we'll refactor to use ostream properly. --- g13d/g13.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/g13d/g13.cc b/g13d/g13.cc index 6f791d9..040fa2f 100644 --- a/g13d/g13.cc +++ b/g13d/g13.cc @@ -32,11 +32,22 @@ void G13_Device::send_event(int type, int code, int val) { _event.type = type; _event.code = code; _event.value = val; - write(_uinput_fid, &_event, sizeof(_event)); + + // TODO(jtgans): Make this actually verify it writes all bytes + auto result = write(_uinput_fid, &_event, sizeof(_event)); + if (result < 0) { + G13_LOG(error, "Unable to send event: " << strerror(errno)); + exit(1); + } } void G13_Device::write_output_pipe(const std::string &out) { - write(_output_pipe_fid, out.c_str(), out.size()); + // TODO(jtgans): Make this actually verify it writes all bytes + auto result = write(_output_pipe_fid, out.c_str(), out.size()); + if (result < 0) { + G13_LOG(error, "Unable to write to output pipe: " << strerror(errno)); + exit(1); + } } void G13_Device::set_mode_leds(int leds) {