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.
This commit is contained in:
ArenM
2021-11-06 17:19:20 -04:00
parent 1f8fa88d37
commit a06360f4c8

View File

@@ -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);