Fix GCC 8 compiler warnings on strncpy's truncated copy on debian distro 98/16898/1
authorSteven Luong <sluong@cisco.com>
Sat, 19 Jan 2019 06:53:18 +0000 (22:53 -0800)
committerSteven Luong <sluong@cisco.com>
Sat, 19 Jan 2019 07:10:11 +0000 (23:10 -0800)
For some reason, GCC 8 in debian is pickier than GCC 8 in ubuntu. It complains
about things in strncpy like this

/home/sluong/vpp/src/vlib/linux/pci.c:485:7: error: ‘strncpy’ output may be
truncated copying 15 bytes from a string of length 255 [-Werror=stringop-truncation]
       strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/sluong/vpp/src/vlib/linux/pci.c: At top level:

It also complains similar things in string_test.c

The fix in pci.c is to convert strncpy to use clib_strncpy
The fix in string_test.c is condiational compile the complained code for GCC 8.

Change-Id: Ic9341ca54ed7407210502197a28283bc42c26662
Signed-off-by: Steven Luong <sluong@cisco.com>
src/plugins/unittest/string_test.c
src/vlib/linux/pci.c

index 41b4c61..cbceb8f 100644 (file)
@@ -750,6 +750,8 @@ test_clib_strncpy (vlib_main_t * vm, unformat_input_t * input)
     return -1;
 
   /* Verify it against strncpy */
+#if __GNUC__ < 8
+  /* GCC 8 debian flunks this one at compile time */
   strncpy (dst, src, strlen (src));
 
   /* This better not fail but check anyhow */
@@ -758,6 +760,7 @@ test_clib_strncpy (vlib_main_t * vm, unformat_input_t * input)
     return -1;
   if (indicator != 0)
     return -1;
+#endif
 
   /* limited copy -- strlen src > n, copy up to n */
   err = clib_strncpy (dst, "The price of greatness is responsibility.", 10);
@@ -791,12 +794,15 @@ test_clib_strncpy (vlib_main_t * vm, unformat_input_t * input)
   if (indicator != 0)
     return -1;
   /* Verify it against strncpy */
+#if __GNUC__ < 8
+  /* GCC 8 debian flunks this one at compile time */
   strncpy (dst, src, strlen (src));
   if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
       EOK)
     return -1;
   if (indicator != 0)
     return -1;
+#endif
 
   /* zero length copy */
   clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
@@ -1046,6 +1052,8 @@ test_strncat_s (vlib_main_t * vm, unformat_input_t * input)
   if (indicator != 0)
     return -1;
   /* verify it against strncat */
+#if __GNUC__ < 8
+  /* GCC 8 debian flunks this one at compile time */
   strcpy_s (dst, sizeof (dst), s1);
   strncat (dst, s2, 13);
   if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
@@ -1053,6 +1061,7 @@ test_strncat_s (vlib_main_t * vm, unformat_input_t * input)
     return -1;
   if (indicator != 0)
     return -1;
+#endif
 
   /* negative stuff */
   err = strncat_s (0, 0, 0, 1);
@@ -1169,6 +1178,8 @@ test_clib_strncat (vlib_main_t * vm, unformat_input_t * input)
   if (indicator != 0)
     return -1;
   /* verify it against strncat */
+#if __GNUC__ < 8
+  /* GCC 8 debian flunks this one at compile time */
   strcpy_s (dst, sizeof (dst), s1);
   strncat (dst, s2, 13);
   if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
@@ -1176,6 +1187,7 @@ test_clib_strncat (vlib_main_t * vm, unformat_input_t * input)
     return -1;
   if (indicator != 0)
     return -1;
+#endif
 
   /* negative stuff */
   err = clib_strncat (0, 0, 1);
index ee3eebf..d31b3c9 100644 (file)
@@ -466,7 +466,7 @@ vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
       clib_memset (&ifr, 0, sizeof ifr);
       clib_memset (&drvinfo, 0, sizeof drvinfo);
       ifr.ifr_data = (char *) &drvinfo;
-      strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
+      clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
 
       drvinfo.cmd = ETHTOOL_GDRVINFO;
       if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
@@ -482,7 +482,7 @@ vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
        continue;
 
       clib_memset (&ifr, 0, sizeof (ifr));
-      strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
+      clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
 
       if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
        {