From d14051e309bcd96d880fc01fd39647cfeaed94ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B6ppner?= Date: Mon, 22 Oct 2018 17:09:10 +0200 Subject: [PATCH] zdev: Copy strings correctly using util_strlcpy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure strings are copied correctly by using util_strlcpy(). Also, rename the NIC_{OWNER,NAME}_LEN definitions to *_SIZE to make it more clear that the terminating null byte ('\0') is included in this buffer size. Using *_LEN would mean only the number of characters in the string fit in. Furthermore, use these definitions rather than sizeof(). This fixes the following GCC8 compile warning: nic.c: In function ‘nic_data_get’: nic.c:58:2: warning: ‘strncpy’ specified bound 9 equals destination size [-Wstringop-truncation] strncpy(data.owner, argv[11], sizeof(data.owner)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ nic.c:59:2: warning: ‘strncpy’ specified bound 9 equals destination size [-Wstringop-truncation] strncpy(data.name, argv[12], sizeof(data.name)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Jan Höppner --- zdev/include/nic.h | 8 ++++---- zdev/src/nic.c | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/zdev/include/nic.h b/zdev/include/nic.h index 1ecc7699..37892e17 100644 --- a/zdev/include/nic.h +++ b/zdev/include/nic.h @@ -24,14 +24,14 @@ enum nic_target { nic_lan, }; -#define NIC_OWNER_LEN 9 -#define NIC_NAME_LEN 9 +#define NIC_OWNER_SIZE 9 +#define NIC_NAME_SIZE 9 struct nic_data { enum nic_type type; enum nic_target target; - char owner[NIC_OWNER_LEN]; - char name[NIC_NAME_LEN]; + char owner[NIC_OWNER_SIZE]; + char name[NIC_NAME_SIZE]; }; bool nic_data_get(const char *, struct nic_data *); diff --git a/zdev/src/nic.c b/zdev/src/nic.c index dd295718..1de57dcd 100644 --- a/zdev/src/nic.c +++ b/zdev/src/nic.c @@ -10,6 +10,8 @@ #include #include +#include "lib/util_libc.h" + #include "misc.h" #include "nic.h" #include "path.h" @@ -55,8 +57,8 @@ bool nic_data_get(const char *id, struct nic_data *data_ptr) else goto out; - strncpy(data.owner, argv[11], sizeof(data.owner)); - strncpy(data.name, argv[12], sizeof(data.name)); + util_strlcpy(data.owner, argv[11], NIC_OWNER_SIZE); + util_strlcpy(data.name, argv[12], NIC_NAME_SIZE); result = true; *data_ptr = data;