From e10d7fe27e10b0ba01ee7698d0b3b5c8adba3d29 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 17 Jun 2020 19:06:28 +0200 Subject: [PATCH] Avoid buffer overflow. Signed-off-by: DL6ER --- src/api/ftl.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/api/ftl.c b/src/api/ftl.c index 80458f10..30d48d73 100644 --- a/src/api/ftl.c +++ b/src/api/ftl.c @@ -134,16 +134,17 @@ void add_to_dnsmasq_log_fifo_buffer(const char *payload, const int length) } // Copy relevant string into temporary buffer - memcpy(fifo_log->message[idx], payload, length); + size_t copybytes = length < MAX_MESSAGE ? length : MAX_MESSAGE; + memcpy(fifo_log->message[idx], payload, copybytes); // Zero-terminate buffer, truncate newline if found - if(fifo_log->message[idx][length - 1u] == '\n') + if(fifo_log->message[idx][copybytes - 1u] == '\n') { - fifo_log->message[idx][length - 1u] = '\0'; + fifo_log->message[idx][copybytes - 1u] = '\0'; } else { - fifo_log->message[idx][length] = '\0'; + fifo_log->message[idx][copybytes] = '\0'; } // Set timestamp @@ -166,9 +167,9 @@ int api_ftl_network(struct mg_connection *conn) { cJSON *json = JSON_NEW_OBJ(); return send_json_error(conn, 500, - "database_error", - "Could not read network details from database table", - json); + "database_error", + "Could not read network details from database table", + json); } // Read record for a single device @@ -192,9 +193,8 @@ int api_ftl_network(struct mg_connection *conn) // Only walk known IP addresses when SELECT query succeeded const char *ipaddr; while((ipaddr = networkTable_readIPsGetRecord()) != NULL) - { JSON_ARRAY_COPY_STR(ip, ipaddr); - } + networkTable_readIPsFinalize(); } JSON_OBJ_ADD_ITEM(item, "ip", ip);