]> granicus.if.org Git - esp-idf/commitdiff
examples/spi_slave: check for truncation in snprintf call
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 28 Aug 2018 13:34:44 +0000 (21:34 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Wed, 29 Aug 2018 06:40:28 +0000 (14:40 +0800)
Also fix character array initializer

examples/peripherals/spi_slave/sender/main/app_main.c

index b1e37dd4965843a251a66906be637944fd3f11ae..c3eb5659272378de4a9fa6d70be320bdaaa1b3a3 100644 (file)
@@ -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