util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] lib/canonicalize: fix truncation warning
@ 2018-05-03 21:57 Sami Kerola
  2018-05-03 21:57 ` [PATCH 2/3] zramctl: " Sami Kerola
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sami Kerola @ 2018-05-03 21:57 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

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.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 lib/canonicalize.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/canonicalize.c b/lib/canonicalize.c
index b600248c7..6cfcf4bca 100644
--- a/lib/canonicalize.c
+++ b/lib/canonicalize.c
@@ -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);
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] zramctl: fix truncation warning
  2018-05-03 21:57 [PATCH 1/3] lib/canonicalize: fix truncation warning Sami Kerola
@ 2018-05-03 21:57 ` Sami Kerola
  2018-05-03 21:57 ` [PATCH 3/3] last: fix false positive compiler warning Sami Kerola
  2018-05-10  9:31 ` [PATCH 1/3] lib/canonicalize: fix truncation warning Karel Zak
  2 siblings, 0 replies; 4+ messages in thread
From: Sami Kerola @ 2018-05-03 21:57 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

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.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/zramctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sys-utils/zramctl.c b/sys-utils/zramctl.c
index 8da7b2ddb..0dc0947e4 100644
--- a/sys-utils/zramctl.c
+++ b/sys-utils/zramctl.c
@@ -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);
 		}
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] last: fix false positive compiler warning
  2018-05-03 21:57 [PATCH 1/3] lib/canonicalize: fix truncation warning Sami Kerola
  2018-05-03 21:57 ` [PATCH 2/3] zramctl: " Sami Kerola
@ 2018-05-03 21:57 ` Sami Kerola
  2018-05-10  9:31 ` [PATCH 1/3] lib/canonicalize: fix truncation warning Karel Zak
  2 siblings, 0 replies; 4+ messages in thread
From: Sami Kerola @ 2018-05-03 21:57 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

login-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.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/last.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/login-utils/last.c b/login-utils/last.c
index 80d77d20b..59dfdb2f5 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -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]))
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] lib/canonicalize: fix truncation warning
  2018-05-03 21:57 [PATCH 1/3] lib/canonicalize: fix truncation warning Sami Kerola
  2018-05-03 21:57 ` [PATCH 2/3] zramctl: " Sami Kerola
  2018-05-03 21:57 ` [PATCH 3/3] last: fix false positive compiler warning Sami Kerola
@ 2018-05-10  9:31 ` Karel Zak
  2 siblings, 0 replies; 4+ messages in thread
From: Karel Zak @ 2018-05-10  9:31 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On Thu, May 03, 2018 at 10:57:57PM +0100, Sami Kerola wrote:
>  lib/canonicalize.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

Applied all 3 patches with small changes, thanks.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-05-10  9:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-03 21:57 [PATCH 1/3] lib/canonicalize: fix truncation warning Sami Kerola
2018-05-03 21:57 ` [PATCH 2/3] zramctl: " Sami Kerola
2018-05-03 21:57 ` [PATCH 3/3] last: fix false positive compiler warning Sami Kerola
2018-05-10  9:31 ` [PATCH 1/3] lib/canonicalize: fix truncation warning Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).