lib/canonicalize: fix truncation warning
Subsystems (2)
PATCH 1/3 lib/canonicalize: fix truncation warning
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.
--- lib/canonicalize.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/canonicalize.c +3 -2
@@ -16,6 +16,7 @@#include <sys/stat.h> #include "canonicalize.h" +#define mapper "/dev/mapper/"/* * 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 (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), mapper"%s" , name);if (access (path,F_OK ) ==0 ) res =strdup (path);
Signature · 1 line
2.17.0
PATCH 2/3 zramctl: fix truncation warning
sys-utils/zramctl.c: In function ‘zram_get_sysfs’:sys-utils/zramctl.c:220:52: warning: ‘%s’ directive output may be truncated writing up to 4095 bytes into a region of size 27 [-Wformat-truncation=]
snprintf(z->devname, sizeof(z->devname), "/dev/%s", name);
As an additional good thing zramctl will no longer allocate 4096 bytes from stack when just 23 bytes is enough.
--- sys-utils/zramctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sys-utils/zramctl.c +1 -1
@@ -215,7 +215,7 @@ static struct sysfs_cxt *zram_get_sysfs(struct zram *z)return NULL ;if (*z->devname !='/' ) {/* canonicalize the device name according to /sys */ -char name[PATH_MAX ]; +char name[sizeof (z->devname) -sizeof ("/dev/" )];if (sysfs_get_devname (&z->sysfs, name,sizeof (name)))snprintf (z->devname,sizeof (z->devname),"/dev/%s" , name); }
Signature · 1 line
2.17.0
PATCH 3/3 last: fix false positive compiler warning
In reply to Sami Kerolalogin-utils/last.c: In function ‘list’:login-utils/last.c:398:36: warning: argument to ‘sizeof’ in ‘strncat’ call is the same expression as the source; did you mean to use the size of the destination? [-Wsizeof-pointer-memaccess]
strncat(utline, p->ut_line, sizeof(p->ut_line));
The sizeof(utline) is defined as sizeof(p->ut_line) + 1, so the compiler got that wrong. Lets truncate strncat() otherway around to keep gcc 8.1 happy.
--- login-utils/last.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
login-utils/last.c +1 -1
@@ -395,7 +395,7 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t* uucp and ftp have special-type entries */ utline[0 ] =0 ; -strncat (utline, p->ut_line,sizeof (p->ut_line)); +strncat (utline, p->ut_line,sizeof (utline) -1 );if (strncmp (utline,"ftp" ,3 ) ==0 &&isdigit (utline[3 ])) utline[3 ] =0 ;if (strncmp (utline,"uucp" ,4 ) ==0 &&isdigit (utline[4 ]))
Signature · 1 line
2.17.0
Quoted 2 lines · Sami Kerola
lib/canonicalize.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Applied all 3 patches with small changes, thanks.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com