From b2a8c02d1da4bd5aa146489f6f7ed2f7a7ae1a81 Mon Sep 17 00:00:00 2001 From: yabu76 Date: Thu, 14 Aug 2025 22:18:41 +0900 Subject: [PATCH] Fix incorrect handling of the return value of the poll system call --- src/misc.c | 7 ++++++- src/xymodem.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/misc.c b/src/misc.c index 13d601e..271d266 100644 --- a/src/misc.c +++ b/src/misc.c @@ -98,12 +98,17 @@ int read_poll(int fd, void *data, size_t len, int timeout) if (fds.revents & POLLIN) { // Read ready data + // return value should not be 0 return read(fd, data, len); } + else /* if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) */ + { + return -1; + } } /* Timeout */ - return ret; + return 0; } // Function to calculate djb2 hash of string diff --git a/src/xymodem.c b/src/xymodem.c index 3b645cc..3b46ef8 100644 --- a/src/xymodem.c +++ b/src/xymodem.c @@ -91,6 +91,9 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) rc = read_poll(sio, &resp, 1, 50); if (rc == 0) { + /* timeout 50 ms + resp has last received character beacuse read_poll() doesn't + destroy resp value in this case. */ if (resp == 'C') break; if (resp == CAN) return ERR; continue; @@ -232,6 +235,9 @@ static int xmodem(int sio, const void *data, size_t len) rc = read_poll(sio, &resp, 1, 50); if (rc == 0) { + /* timeout 50 ms + resp has last received character beacuse read_poll() doesn't + destroy resp value in this case. */ if (resp == 'C') break; if (resp == CAN) return ERR; continue; @@ -389,6 +395,10 @@ int start_receive(int sio) { return rc; } + else /* if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) */ + { + return -1; + } } if (key_hit) return USER_CAN; @@ -544,6 +554,10 @@ int receive_packet(int sio, struct xpacket packet, int fd) while (read_poll(sio, &dummy, 1, 100) > 0) {} return ERR; } + else /* if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) */ + { + return ERR; + } } uint8_t tester = 0xff;