From 9c67c66ac0e0308306edf54f20f5e6668fae11fb Mon Sep 17 00:00:00 2001 From: Alexander Aring Date: Jul 14 2020 18:14:16 +0000 Subject: dlm_controld: fix -Wstringop-truncation warnings This patch fixes in dlm_controld all -Wstringop-truncation warnings. There exists two different cases inside the code: 1. string buffer without null termination: Code work as expected in this case a pragma is introduced to ignore the warning. 2. string buffer with null termination: The function strncpy() will not leave the destination buffer with a null termination symbol if the buffer doesn't fit. That's why gcc above 8.0 print warnings. Obviously there are some memset() to zero the buffer and doing a strncpy() minus one of the buffer length afterwards which seems fine. The fact that gcc still complains and knowing other discussions about memset() I believe that there might be reasons why gcc doesn't stop to complain about such code or gcc isn't able to detect it. However this patch will guarantee that the destination buffer is always null terminated and the full destination buffer size is used now. --- diff --git a/dlm_controld/cpg.c b/dlm_controld/cpg.c index 5b5c52f..f3365ee 100644 --- a/dlm_controld/cpg.c +++ b/dlm_controld/cpg.c @@ -1867,7 +1867,8 @@ int set_lockspace_info(struct lockspace *ls, struct dlmc_lockspace *lockspace) { struct change *cg, *last = NULL; - strncpy(lockspace->name, ls->name, DLM_LOCKSPACE_LEN); + strncpy(lockspace->name, ls->name, DLM_LOCKSPACE_LEN + 1); + lockspace->name[DLM_LOCKSPACE_LEN] = '\0'; lockspace->global_id = ls->global_id; if (ls->joining) diff --git a/dlm_controld/fence_config.c b/dlm_controld/fence_config.c index 5d8d7dc..08996ac 100644 --- a/dlm_controld/fence_config.c +++ b/dlm_controld/fence_config.c @@ -180,11 +180,21 @@ static int read_config_section(unsigned int nodeid, FILE *file, char *dev_line, memset(dev, 0, sizeof(struct fence_device)); memset(con, 0, sizeof(struct fence_connect)); - strncpy(dev->name, dev_name, FENCE_CONFIG_NAME_MAX-1); - strncpy(dev->agent, agent, FENCE_CONFIG_NAME_MAX-1); - strncpy(dev->args, dev_args, FENCE_CONFIG_ARGS_MAX-1); - strncpy(con->name, con_name, FENCE_CONFIG_NAME_MAX-1); - strncpy(con->args, con_args, FENCE_CONFIG_ARGS_MAX-1); + strncpy(dev->name, dev_name, FENCE_CONFIG_NAME_MAX); + dev->name[FENCE_CONFIG_NAME_MAX - 1] = '\0'; + + strncpy(dev->agent, agent, FENCE_CONFIG_NAME_MAX); + dev->agent[FENCE_CONFIG_NAME_MAX - 1] = '\0'; + + strncpy(dev->args, dev_args, FENCE_CONFIG_ARGS_MAX); + dev->args[FENCE_CONFIG_ARGS_MAX - 1] = '\0'; + + strncpy(con->name, con_name, FENCE_CONFIG_NAME_MAX); + con->name[FENCE_CONFIG_NAME_MAX - 1] = '\0'; + + strncpy(con->args, con_args, FENCE_CONFIG_ARGS_MAX); + con->args[FENCE_CONFIG_ARGS_MAX - 1] = '\0'; + dev->unfence = unfence; *dev_out = dev; diff --git a/dlm_controld/lib.c b/dlm_controld/lib.c index b6ea3a3..53c11cf 100644 --- a/dlm_controld/lib.c +++ b/dlm_controld/lib.c @@ -81,6 +81,17 @@ static int do_connect(const char *sock_path) return fd; } +static inline void init_header_name(struct dlmc_header *h, + const char *name, size_t len) +{ +#pragma GCC diagnostic push +#if __GNUC__ >= 8 +#pragma GCC diagnostic ignored "-Wstringop-truncation" +#endif + strncpy(h->name, name, len); +#pragma GCC diagnostic pop +} + static void init_header(struct dlmc_header *h, int cmd, char *name, int extra_len) { @@ -92,7 +103,7 @@ static void init_header(struct dlmc_header *h, int cmd, char *name, h->command = cmd; if (name) - strncpy(h->name, name, DLM_LOCKSPACE_LEN); + init_header_name(h, name, DLM_LOCKSPACE_LEN); } static char copy_buf[DLMC_DUMP_SIZE]; @@ -881,7 +892,7 @@ int dlmc_run_check(char *run_uuid, int len, int wait_sec, uint32_t flags, init_header(&h, DLMC_CMD_RUN_CHECK, NULL, 0); h.flags = flags; - strncpy(h.name, run_uuid, DLMC_RUN_UUID_LEN); + init_header_name(&h, run_uuid, DLMC_RUN_UUID_LEN); memset(&rh, 0, sizeof(rh)); diff --git a/dlm_controld/main.c b/dlm_controld/main.c index 8023f4b..645bd26 100644 --- a/dlm_controld/main.c +++ b/dlm_controld/main.c @@ -788,6 +788,17 @@ static int setup_uevent(void) return s; } +static inline void init_header_name(struct dlmc_header *h, + const char *name, size_t len) +{ +#pragma GCC diagnostic push +#if __GNUC__ >= 8 +#pragma GCC diagnostic ignored "-Wstringop-truncation" +#endif + strncpy(h->name, name, len); +#pragma GCC diagnostic pop +} + static void init_header(struct dlmc_header *h, int cmd, char *name, int result, int extra_len) { @@ -800,7 +811,7 @@ static void init_header(struct dlmc_header *h, int cmd, char *name, int result, h->data = result; if (name) - strncpy(h->name, name, DLM_LOCKSPACE_LEN); + init_header_name(h, name, DLM_LOCKSPACE_LEN); } static char copy_buf[LOG_DUMP_SIZE];