]> granicus.if.org Git - sysstat/commitdiff
Fix GCC v10 warnings
authorSebastien GODARD <sysstat@users.noreply.github.com>
Sun, 3 May 2020 07:05:58 +0000 (09:05 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Sun, 3 May 2020 07:05:58 +0000 (09:05 +0200)
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 <sysstat@users.noreply.github.com>
common.c
ioconf.c

index 75794927064f264542c5d8e0431d646abd21ef36..28279ceb3027ef65d3376400cf188a56b9c173c8 100644 (file)
--- 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);
        }
 
index a9375c96d77ce2a72be19fdbad3e664075e80c85..54d522bd27950c20b8e7e450fba922dd49e7dae1 100644 (file)
--- 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 {