From c3123b00aedf8f3e7e43933be7f74e98947e013d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 28 Aug 2018 21:34:44 +0800 Subject: [PATCH] examples/spi_slave: check for truncation in snprintf call Also fix character array initializer --- .../peripherals/spi_slave/sender/main/app_main.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/peripherals/spi_slave/sender/main/app_main.c b/examples/peripherals/spi_slave/sender/main/app_main.c index b1e37dd496..c3eb565927 100644 --- a/examples/peripherals/spi_slave/sender/main/app_main.c +++ b/examples/peripherals/spi_slave/sender/main/app_main.c @@ -118,8 +118,8 @@ void app_main() }; int n=0; - char sendbuf[128]=""; - char recvbuf[128]=""; + char sendbuf[128] = {0}; + char recvbuf[128] = {0}; spi_transaction_t t; memset(&t, 0, sizeof(t)); @@ -143,8 +143,12 @@ void app_main() xSemaphoreGive(rdySem); while(1) { - snprintf(sendbuf, 128, "Sender, transmission no. %04i. Last time, I received: \"%s\"", n, recvbuf); - t.length=128*8; //128 bytes + int res = snprintf(sendbuf, sizeof(sendbuf), + "Sender, transmission no. %04i. Last time, I received: \"%s\"", n, recvbuf); + if (res >= sizeof(sendbuf)) { + printf("Data truncated\n"); + } + t.length=sizeof(sendbuf)*8; t.tx_buffer=sendbuf; t.rx_buffer=recvbuf; //Wait for slave to be ready for next byte before sending