From a06360f4c84730865f2e08a39d907b287a24755b Mon Sep 17 00:00:00 2001 From: ArenM Date: Sat, 6 Nov 2021 17:19:20 -0400 Subject: [PATCH] at: break before overflow when receiving messages Previously this code checked if the buffer was full after writing to it, which meant that the buffer could overflow. This checks for an overflow before copying into the buffer and only copies the data that will fit. --- src/at.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/at.c b/src/at.c index ee99ff4..56dbe76 100644 --- a/src/at.c +++ b/src/at.c @@ -245,7 +245,14 @@ static gboolean modem_response(gint fd, */ do { ret = read(fd, tmp, sizeof(tmp)); + if (ret > 0) { + /* If we're going to overflow truncate the data we read to fit */ + if (pos + ret >= sizeof(response)) { + g_critical("AT response buffer full, truncating"); + ret = sizeof(response) - (pos + 1); + } + memcpy(&response[pos], tmp, ret); pos += ret; usleep(10000);