Fix some strncpy/strcat gcc 8.2 warnings by using libbsd strlcpy/strlcat
diff mbox series

Message ID 20180817181206.21869-1-colin.king@canonical.com
State Accepted
Headers show
Series
  • Fix some strncpy/strcat gcc 8.2 warnings by using libbsd strlcpy/strlcat
Related show

Commit Message

Colin King Aug. 17, 2018, 6:12 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

Fix build issues on Debian Sid with gcc 8.2.0-3 - use the safer
strl* functions rather than strn* variants.

Thanks to Paul Menzel for reporting these issues.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 configure.ac               | 1 +
 debian/control             | 2 +-
 src/Makefile.am            | 2 +-
 src/acpi/crsdump/crsdump.c | 3 ++-
 src/lib/src/Makefile.am    | 2 +-
 src/lib/src/fwts_hwinfo.c  | 8 +++++---
 6 files changed, 11 insertions(+), 7 deletions(-)

Comments

Alex Hung Aug. 17, 2018, 10 p.m. UTC | #1
On 2018-08-17 11:12 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> Fix build issues on Debian Sid with gcc 8.2.0-3 - use the safer
> strl* functions rather than strn* variants.
> 
> Thanks to Paul Menzel for reporting these issues.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   configure.ac               | 1 +
>   debian/control             | 2 +-
>   src/Makefile.am            | 2 +-
>   src/acpi/crsdump/crsdump.c | 3 ++-
>   src/lib/src/Makefile.am    | 2 +-
>   src/lib/src/fwts_hwinfo.c  | 8 +++++---
>   6 files changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/configure.ac b/configure.ac
> index d7e0aee7..0109d644 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -46,6 +46,7 @@
>   	  AC_CHECK_HEADERS([stdio.h])
>   	  AC_CHECK_HEADERS([stdlib.h])
>   	  AC_CHECK_HEADERS([string.h])
> +	  AC_CHECK_HEADERS([bsd/string.h])
>   	  AC_CHECK_HEADERS([sys/io.h])
>   	  AC_CHECK_HEADERS([sys/klog.h])
>   	  AC_CHECK_HEADERS([sys/mman.h])
> diff --git a/debian/control b/debian/control
> index c38aa473..dcc7c190 100644
> --- a/debian/control
> +++ b/debian/control
> @@ -4,7 +4,7 @@ Priority: optional
>   Maintainer: Firmware Testing Team <fwts-devel@lists.ubuntu.com>
>   Uploaders: Colin King <colin.king@ubuntu.com>, Alex Hung <alex.hung@canonical.com>, Ivan Hu <ivan.hu@canonical.com>
>   Standards-Version: 3.9.8
> -Build-Depends: debhelper (>= 7.0.50~), autoconf, automake, libtool, libjson0-dev | libjson-c-dev (>= 0.9), flex, bison, dh-autoreconf, dkms, libglib2.0-dev, pkg-config, libfdt-dev
> +Build-Depends: debhelper (>= 7.0.50~), autoconf, automake, libtool, libjson0-dev | libjson-c-dev (>= 0.9), flex, bison, dh-autoreconf, dkms, libglib2.0-dev, pkg-config, libfdt-dev, libbsd-dev
>   
>   Package: fwts
>   Architecture: i386 amd64 armel armhf arm64 ppc64 ppc64el s390x
> diff --git a/src/Makefile.am b/src/Makefile.am
> index b2ea8830..52c637dd 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -192,7 +192,7 @@ fwts_SOURCES = main.c 				\
>   	$(power_mgmt_tests)			\
>   	$(dt_tests)
>   
> -fwts_LDFLAGS = -lm `pkg-config --libs glib-2.0 gio-2.0`
> +fwts_LDFLAGS = -lm -lbsd `pkg-config --libs glib-2.0 gio-2.0`
>   
>   fwts_LDADD = \
>   	-L$(top_builddir)/src/acpica -lfwtsacpica \
> diff --git a/src/acpi/crsdump/crsdump.c b/src/acpi/crsdump/crsdump.c
> index 706677c4..bf8e3faf 100644
> --- a/src/acpi/crsdump/crsdump.c
> +++ b/src/acpi/crsdump/crsdump.c
> @@ -23,6 +23,7 @@
>   #include <stdio.h>
>   #include <stdlib.h>
>   #include <string.h>
> +#include <bsd/string.h>
>   #include <unistd.h>
>   #include <inttypes.h>
>   #include "fwts_acpi_object_eval.h"
> @@ -320,7 +321,7 @@ static const char *crs_irq_map(const uint64_t val)
>   				char tmp[5];
>   
>   				snprintf(tmp, sizeof(tmp), " %u", i);
> -				strncat(buf, tmp, 4);
> +				strlcat(buf, tmp, sizeof(tmp));
>   			}
>   		}
>   	}
> diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am
> index 35fde99b..54de7f44 100644
> --- a/src/lib/src/Makefile.am
> +++ b/src/lib/src/Makefile.am
> @@ -29,7 +29,7 @@ AM_CPPFLAGS = \
>   pkglib_LTLIBRARIES = libfwts.la
>   
>   libfwts_la_LDFLAGS = 			\
> -	-lm -lpthread 			\
> +	-lm -lpthread -lbsd		\
>   	-version-info 1:0:0 		\
>   	-L$(top_builddir)/src/acpica/source/compiler \
>   	-lfwtsiasl `pkg-config --libs glib-2.0 gio-2.0`
> diff --git a/src/lib/src/fwts_hwinfo.c b/src/lib/src/fwts_hwinfo.c
> index f4ab8f51..7324f8d2 100644
> --- a/src/lib/src/fwts_hwinfo.c
> +++ b/src/lib/src/fwts_hwinfo.c
> @@ -19,6 +19,8 @@
>   
>   #include <stdlib.h>
>   #include <stdint.h>
> +#include <string.h>
> +#include <bsd/string.h>
>   #include <errno.h>
>   #include <inttypes.h>
>   #include <dirent.h>
> @@ -397,7 +399,7 @@ static int fwts_hwinfo_net_get(
>   			break;
>   		}
>   		memset(&buf, 0, sizeof(buf));
> -		strncpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name) - 1);
> +		strlcpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name));
>   		if (ioctl(sock, SIOCGIFHWADDR, &buf) < 0) {
>   			fwts_log_error(fw, "Cannot get network information for device %s.", d->d_name);
>   			fwts_hwinfo_net_free(net_config);
> @@ -410,7 +412,7 @@ static int fwts_hwinfo_net_get(
>   			break;
>   		}
>   		memset(&buf, 0, sizeof(buf));
> -		strncpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name) - 1);
> +		strlcpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name));
>   		if (ioctl(sock, SIOCGIFADDR, &buf) < 0) {
>   			if (errno != EADDRNOTAVAIL)
>   				fwts_log_error(fw, "Cannot get address for device %s.", d->d_name);
> @@ -500,7 +502,7 @@ static int fwts_hwinfo_pci_get(
>   		}
>   		memcpy(pci_config->config, config, n);
>   		pci_config->config_len = n;
> -		strncpy(pci_config->name, d->d_name, NAME_MAX);
> +		strlcpy(pci_config->name, d->d_name, sizeof(pci_config->name));
>   
>   		fwts_list_append(configs, pci_config);
>   	}
> 


Acked-by: Alex Hung <alex.hung@canonical.com>
ivanhu Aug. 20, 2018, 5:41 a.m. UTC | #2
On 08/18/2018 02:12 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Fix build issues on Debian Sid with gcc 8.2.0-3 - use the safer
> strl* functions rather than strn* variants.
>
> Thanks to Paul Menzel for reporting these issues.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  configure.ac               | 1 +
>  debian/control             | 2 +-
>  src/Makefile.am            | 2 +-
>  src/acpi/crsdump/crsdump.c | 3 ++-
>  src/lib/src/Makefile.am    | 2 +-
>  src/lib/src/fwts_hwinfo.c  | 8 +++++---
>  6 files changed, 11 insertions(+), 7 deletions(-)
>
> diff --git a/configure.ac b/configure.ac
> index d7e0aee7..0109d644 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -46,6 +46,7 @@
>  	  AC_CHECK_HEADERS([stdio.h])
>  	  AC_CHECK_HEADERS([stdlib.h])
>  	  AC_CHECK_HEADERS([string.h])
> +	  AC_CHECK_HEADERS([bsd/string.h])
>  	  AC_CHECK_HEADERS([sys/io.h])
>  	  AC_CHECK_HEADERS([sys/klog.h])
>  	  AC_CHECK_HEADERS([sys/mman.h])
> diff --git a/debian/control b/debian/control
> index c38aa473..dcc7c190 100644
> --- a/debian/control
> +++ b/debian/control
> @@ -4,7 +4,7 @@ Priority: optional
>  Maintainer: Firmware Testing Team <fwts-devel@lists.ubuntu.com>
>  Uploaders: Colin King <colin.king@ubuntu.com>, Alex Hung <alex.hung@canonical.com>, Ivan Hu <ivan.hu@canonical.com>
>  Standards-Version: 3.9.8
> -Build-Depends: debhelper (>= 7.0.50~), autoconf, automake, libtool, libjson0-dev | libjson-c-dev (>= 0.9), flex, bison, dh-autoreconf, dkms, libglib2.0-dev, pkg-config, libfdt-dev
> +Build-Depends: debhelper (>= 7.0.50~), autoconf, automake, libtool, libjson0-dev | libjson-c-dev (>= 0.9), flex, bison, dh-autoreconf, dkms, libglib2.0-dev, pkg-config, libfdt-dev, libbsd-dev
>  
>  Package: fwts
>  Architecture: i386 amd64 armel armhf arm64 ppc64 ppc64el s390x
> diff --git a/src/Makefile.am b/src/Makefile.am
> index b2ea8830..52c637dd 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -192,7 +192,7 @@ fwts_SOURCES = main.c 				\
>  	$(power_mgmt_tests)			\
>  	$(dt_tests)
>  
> -fwts_LDFLAGS = -lm `pkg-config --libs glib-2.0 gio-2.0`
> +fwts_LDFLAGS = -lm -lbsd `pkg-config --libs glib-2.0 gio-2.0`
>  
>  fwts_LDADD = \
>  	-L$(top_builddir)/src/acpica -lfwtsacpica \
> diff --git a/src/acpi/crsdump/crsdump.c b/src/acpi/crsdump/crsdump.c
> index 706677c4..bf8e3faf 100644
> --- a/src/acpi/crsdump/crsdump.c
> +++ b/src/acpi/crsdump/crsdump.c
> @@ -23,6 +23,7 @@
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> +#include <bsd/string.h>
>  #include <unistd.h>
>  #include <inttypes.h>
>  #include "fwts_acpi_object_eval.h"
> @@ -320,7 +321,7 @@ static const char *crs_irq_map(const uint64_t val)
>  				char tmp[5];
>  
>  				snprintf(tmp, sizeof(tmp), " %u", i);
> -				strncat(buf, tmp, 4);
> +				strlcat(buf, tmp, sizeof(tmp));
>  			}
>  		}
>  	}
> diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am
> index 35fde99b..54de7f44 100644
> --- a/src/lib/src/Makefile.am
> +++ b/src/lib/src/Makefile.am
> @@ -29,7 +29,7 @@ AM_CPPFLAGS = \
>  pkglib_LTLIBRARIES = libfwts.la
>  
>  libfwts_la_LDFLAGS = 			\
> -	-lm -lpthread 			\
> +	-lm -lpthread -lbsd		\
>  	-version-info 1:0:0 		\
>  	-L$(top_builddir)/src/acpica/source/compiler \
>  	-lfwtsiasl `pkg-config --libs glib-2.0 gio-2.0`
> diff --git a/src/lib/src/fwts_hwinfo.c b/src/lib/src/fwts_hwinfo.c
> index f4ab8f51..7324f8d2 100644
> --- a/src/lib/src/fwts_hwinfo.c
> +++ b/src/lib/src/fwts_hwinfo.c
> @@ -19,6 +19,8 @@
>  
>  #include <stdlib.h>
>  #include <stdint.h>
> +#include <string.h>
> +#include <bsd/string.h>
>  #include <errno.h>
>  #include <inttypes.h>
>  #include <dirent.h>
> @@ -397,7 +399,7 @@ static int fwts_hwinfo_net_get(
>  			break;
>  		}
>  		memset(&buf, 0, sizeof(buf));
> -		strncpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name) - 1);
> +		strlcpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name));
>  		if (ioctl(sock, SIOCGIFHWADDR, &buf) < 0) {
>  			fwts_log_error(fw, "Cannot get network information for device %s.", d->d_name);
>  			fwts_hwinfo_net_free(net_config);
> @@ -410,7 +412,7 @@ static int fwts_hwinfo_net_get(
>  			break;
>  		}
>  		memset(&buf, 0, sizeof(buf));
> -		strncpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name) - 1);
> +		strlcpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name));
>  		if (ioctl(sock, SIOCGIFADDR, &buf) < 0) {
>  			if (errno != EADDRNOTAVAIL)
>  				fwts_log_error(fw, "Cannot get address for device %s.", d->d_name);
> @@ -500,7 +502,7 @@ static int fwts_hwinfo_pci_get(
>  		}
>  		memcpy(pci_config->config, config, n);
>  		pci_config->config_len = n;
> -		strncpy(pci_config->name, d->d_name, NAME_MAX);
> +		strlcpy(pci_config->name, d->d_name, sizeof(pci_config->name));
>  
>  		fwts_list_append(configs, pci_config);
>  	}

Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch
diff mbox series

diff --git a/configure.ac b/configure.ac
index d7e0aee7..0109d644 100644
--- a/configure.ac
+++ b/configure.ac
@@ -46,6 +46,7 @@ 
 	  AC_CHECK_HEADERS([stdio.h])
 	  AC_CHECK_HEADERS([stdlib.h])
 	  AC_CHECK_HEADERS([string.h])
+	  AC_CHECK_HEADERS([bsd/string.h])
 	  AC_CHECK_HEADERS([sys/io.h])
 	  AC_CHECK_HEADERS([sys/klog.h])
 	  AC_CHECK_HEADERS([sys/mman.h])
diff --git a/debian/control b/debian/control
index c38aa473..dcc7c190 100644
--- a/debian/control
+++ b/debian/control
@@ -4,7 +4,7 @@  Priority: optional
 Maintainer: Firmware Testing Team <fwts-devel@lists.ubuntu.com>
 Uploaders: Colin King <colin.king@ubuntu.com>, Alex Hung <alex.hung@canonical.com>, Ivan Hu <ivan.hu@canonical.com>
 Standards-Version: 3.9.8
-Build-Depends: debhelper (>= 7.0.50~), autoconf, automake, libtool, libjson0-dev | libjson-c-dev (>= 0.9), flex, bison, dh-autoreconf, dkms, libglib2.0-dev, pkg-config, libfdt-dev
+Build-Depends: debhelper (>= 7.0.50~), autoconf, automake, libtool, libjson0-dev | libjson-c-dev (>= 0.9), flex, bison, dh-autoreconf, dkms, libglib2.0-dev, pkg-config, libfdt-dev, libbsd-dev
 
 Package: fwts
 Architecture: i386 amd64 armel armhf arm64 ppc64 ppc64el s390x
diff --git a/src/Makefile.am b/src/Makefile.am
index b2ea8830..52c637dd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -192,7 +192,7 @@  fwts_SOURCES = main.c 				\
 	$(power_mgmt_tests)			\
 	$(dt_tests)
 
-fwts_LDFLAGS = -lm `pkg-config --libs glib-2.0 gio-2.0`
+fwts_LDFLAGS = -lm -lbsd `pkg-config --libs glib-2.0 gio-2.0`
 
 fwts_LDADD = \
 	-L$(top_builddir)/src/acpica -lfwtsacpica \
diff --git a/src/acpi/crsdump/crsdump.c b/src/acpi/crsdump/crsdump.c
index 706677c4..bf8e3faf 100644
--- a/src/acpi/crsdump/crsdump.c
+++ b/src/acpi/crsdump/crsdump.c
@@ -23,6 +23,7 @@ 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <bsd/string.h>
 #include <unistd.h>
 #include <inttypes.h>
 #include "fwts_acpi_object_eval.h"
@@ -320,7 +321,7 @@  static const char *crs_irq_map(const uint64_t val)
 				char tmp[5];
 
 				snprintf(tmp, sizeof(tmp), " %u", i);
-				strncat(buf, tmp, 4);
+				strlcat(buf, tmp, sizeof(tmp));
 			}
 		}
 	}
diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am
index 35fde99b..54de7f44 100644
--- a/src/lib/src/Makefile.am
+++ b/src/lib/src/Makefile.am
@@ -29,7 +29,7 @@  AM_CPPFLAGS = \
 pkglib_LTLIBRARIES = libfwts.la
 
 libfwts_la_LDFLAGS = 			\
-	-lm -lpthread 			\
+	-lm -lpthread -lbsd		\
 	-version-info 1:0:0 		\
 	-L$(top_builddir)/src/acpica/source/compiler \
 	-lfwtsiasl `pkg-config --libs glib-2.0 gio-2.0`
diff --git a/src/lib/src/fwts_hwinfo.c b/src/lib/src/fwts_hwinfo.c
index f4ab8f51..7324f8d2 100644
--- a/src/lib/src/fwts_hwinfo.c
+++ b/src/lib/src/fwts_hwinfo.c
@@ -19,6 +19,8 @@ 
 
 #include <stdlib.h>
 #include <stdint.h>
+#include <string.h>
+#include <bsd/string.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <dirent.h>
@@ -397,7 +399,7 @@  static int fwts_hwinfo_net_get(
 			break;
 		}
 		memset(&buf, 0, sizeof(buf));
-		strncpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name) - 1);
+		strlcpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name));
 		if (ioctl(sock, SIOCGIFHWADDR, &buf) < 0) {
 			fwts_log_error(fw, "Cannot get network information for device %s.", d->d_name);
 			fwts_hwinfo_net_free(net_config);
@@ -410,7 +412,7 @@  static int fwts_hwinfo_net_get(
 			break;
 		}
 		memset(&buf, 0, sizeof(buf));
-		strncpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name) - 1);
+		strlcpy(buf.ifr_name, d->d_name, sizeof(buf.ifr_name));
 		if (ioctl(sock, SIOCGIFADDR, &buf) < 0) {
 			if (errno != EADDRNOTAVAIL)
 				fwts_log_error(fw, "Cannot get address for device %s.", d->d_name);
@@ -500,7 +502,7 @@  static int fwts_hwinfo_pci_get(
 		}
 		memcpy(pci_config->config, config, n);
 		pci_config->config_len = n;
-		strncpy(pci_config->name, d->d_name, NAME_MAX);
+		strlcpy(pci_config->name, d->d_name, sizeof(pci_config->name));
 
 		fwts_list_append(configs, pci_config);
 	}