net: tftp: Ensure the error message fits into transmit buffer

Make sure that the error message does not overflow the transmit buffer
when copying the error message string.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2023-11-17 12:53:38 +01:00 committed by Carles Cufí
parent 59544d58ef
commit f0247131bf

View file

@ -155,8 +155,14 @@ static inline int send_err(int sock, struct tftpc *client, int err_code, char *e
/* Copy the Error String. */
if (err_msg != NULL) {
strcpy(client->tftp_buf + req_size, err_msg);
req_size += strlen(err_msg);
size_t copy_len = strlen(err_msg);
if (copy_len > sizeof(client->tftp_buf) - req_size) {
copy_len = sizeof(client->tftp_buf) - req_size;
}
memcpy(client->tftp_buf + req_size, err_msg, copy_len);
req_size += copy_len;
}
/* Send Error to server. */