From e75673f820213771803ce7f24745346a39934d03 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sun, 3 May 2020 09:05:58 +0200 Subject: [PATCH] Fix GCC v10 warnings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit GCC versions 9 and later complain more agressively, e.g.: common.c: In function ‘get_wwnid_from_pretty’: common.c:396:4: warning: ‘strncpy’ offset [275, 4095] from the object at ‘drd’ is out of the bounds of referenced subobject ‘d_name’ with type ‘char[256]’ at offset 19 [-Warray-bounds] 396 | strncpy(wwn_name, drd->d_name, sizeof(wwn_name)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/dirent.h:61, from common.c:32: /usr/include/bits/dirent.h:33:10: note: subobject ‘d_name’ declared here 33 | char d_name[256]; /* We must not include limits.h! */ | ^~~~~~ or: common.c: In function ‘get_persistent_name_path’: common.c:876:37: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=] 876 | snprintf(path, sizeof(path), "%s/%s", | ^ common.c:876:2: note: ‘snprintf’ output 2 or more bytes (assuming 4097) into a destination of size 4096 876 | snprintf(path, sizeof(path), "%s/%s", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 877 | get_persistent_type_dir(persistent_name_type), name); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This patch quiets GCC. Signed-off-by: Sebastien GODARD --- common.c | 20 ++++++++++++-------- ioconf.c | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/common.c b/common.c index 7579492..28279ce 100644 --- a/common.c +++ b/common.c @@ -393,7 +393,7 @@ int get_wwnid_from_pretty(char *pretty, unsigned long long *wwn, unsigned int *p if (!strncmp(name, pretty, FILENAME_MAX)) { /* We have found pretty name for current persistent one */ - strncpy(wwn_name, drd->d_name, sizeof(wwn_name)); + strncpy(wwn_name, drd->d_name, MINIMUM(sizeof(wwn_name), sizeof(drd->d_name))); wwn_name[sizeof(wwn_name) - 1] = '\0'; /* Try to extract WWN */ @@ -842,16 +842,18 @@ char *strtolower(char *str) * @type Persistent type name (UUID, LABEL, etc.) * * RETURNS: - * Path to the persistent type name directory, or NULL if access is denied. + * Path to the persistent type name directory, or NULL if access is denied + * or strings have been truncated. *************************************************************************** */ char *get_persistent_type_dir(char *type) { static char dir[PATH_MAX]; + int n; - snprintf(dir, sizeof(dir), "%s-%s", DEV_DISK_BY, type); + n = snprintf(dir, sizeof(dir), "%s-%s", DEV_DISK_BY, type); - if (access(dir, R_OK)) { + if ((n >= sizeof(dir)) || access(dir, R_OK)) { return (NULL); } @@ -866,17 +868,19 @@ char *get_persistent_type_dir(char *type) * @name Persistent name. * * RETURNS: - * Path to the persistent name, or NULL if file doesn't exist. + * Path to the persistent name, or NULL if file doesn't exist or strings + * have been truncated. *************************************************************************** */ char *get_persistent_name_path(char *name) { static char path[PATH_MAX]; + int n; - snprintf(path, sizeof(path), "%s/%s", - get_persistent_type_dir(persistent_name_type), name); + n = snprintf(path, sizeof(path), "%s/%s", + get_persistent_type_dir(persistent_name_type), name); - if (access(path, F_OK)) { + if ((n >= sizeof(path)) || access(path, F_OK)) { return (NULL); } diff --git a/ioconf.c b/ioconf.c index a9375c9..54d522b 100644 --- a/ioconf.c +++ b/ioconf.c @@ -301,7 +301,7 @@ int ioc_init(void) /* basename of device + provided string + controller # */ if (*cfmt == '*') { - strncpy(blkp->cfmt, blkp->name, sizeof(blkp->cfmt) - 1); + strncpy(blkp->cfmt, blkp->name, MINIMUM(sizeof(blkp->name), sizeof(blkp->cfmt) - 1)); blkp->cfmt[sizeof(blkp->cfmt) - 1] = '\0'; } else { -- 2.40.0