]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/canonicalize: fix truncation warning
authorSami Kerola <kerolasa@iki.fi>
Thu, 3 May 2018 21:57:57 +0000 (22:57 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 10 May 2018 09:20:56 +0000 (11:20 +0200)
lib/canonicalize.c: In function ‘canonicalize_dm_name’:
lib/canonicalize.c:42:45: warning: ‘%s’ directive output may be truncated
writing up to 255 bytes into a region of size 244 [-Wformat-truncation=]
   snprintf(path, sizeof(path), "/dev/mapper/%s", name);

Notice that this warnign fix does not improve code enormously.  The earlier
snprintf() truncation will not happen a bit earlier when fgets() is called.
In that sense this change merely makes one easy to silence warning to
disappear, and therefore improve change of noticing useful messaging as such
crops up.

[kzak@redhat.com: - use macro rather than hardcoded string for mapper path]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/pathnames.h
lib/canonicalize.c

index 59cc66736216eb15614299a3bbe2048e790e0cea..abf0cdee98b71140605a0123febb3faa3e1cb710 100644 (file)
 # define _PATH_DEV             "/dev/"
 #endif
 
+#define _PATH_DEV_MAPPER       "/dev/mapper"
+
 #define _PATH_DEV_MEM          "/dev/mem"
 
 #define _PATH_DEV_LOOP         "/dev/loop"
index b600248c717f1599985b80369ed420a3478332d6..ef3342b12b1e5294bcf1ab3fe76f8f177c7480de 100644 (file)
@@ -16,6 +16,7 @@
 #include <sys/stat.h>
 
 #include "canonicalize.h"
+#include "pathnames.h"
 
 /*
  * Converts private "dm-N" names to "/dev/mapper/<name>"
@@ -27,7 +28,7 @@ char *canonicalize_dm_name(const char *ptname)
 {
        FILE    *f;
        size_t  sz;
-       char    path[256], name[256], *res = NULL;
+       char    path[256], name[sizeof(path) - sizeof(_PATH_DEV_MAPPER)], *res = NULL;
 
        if (!ptname || !*ptname)
                return NULL;
@@ -39,7 +40,7 @@ char *canonicalize_dm_name(const char *ptname)
        /* read "<name>\n" from sysfs */
        if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
                name[sz - 1] = '\0';
-               snprintf(path, sizeof(path), "/dev/mapper/%s", name);
+               snprintf(path, sizeof(path), _PATH_DEV_MAPPER "/%s", name);
 
                if (access(path, F_OK) == 0)
                        res = strdup(path);