Util-Linux package development
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>
Subject: [PATCH 1/7] cfdisk: avoid use of VLA in combination with sizeof() [smatch scan]
Date: Mon, 13 Feb 2017 22:06:35 +0000	[thread overview]
Message-ID: <20170213220641.1395-2-kerolasa@iki.fi> (raw)
In-Reply-To: <20170213220641.1395-1-kerolasa@iki.fi>

disk-utils/cfdisk.c:1066:29: error: cannot size expression

One should use sizeof() only when variable size can be known at time of
compilation.  That is not the case with variable length arrays.

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

diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c
index 281d66c78..ce49a791c 100644
--- a/disk-utils/cfdisk.c
+++ b/disk-utils/cfdisk.c
@@ -1044,9 +1044,10 @@ static void ui_draw_menuitem(struct cfdisk *cf,
 			     struct cfdisk_menuitem *d,
 			     size_t idx)
 {
-	char buf[80 * MB_CUR_MAX], *ptr = buf;
+	char *buf, *ptr;
 	const char *name;
 	size_t width;
+	const size_t buf_sz = 80 * MB_CUR_MAX;
 	int ln, cl, vert = cf->menu->vertical;
 
 	if (!menuitem_on_page(cf, idx))
@@ -1054,6 +1055,7 @@ static void ui_draw_menuitem(struct cfdisk *cf,
 	ln = menuitem_get_line(cf, idx);
 	cl = menuitem_get_column(cf, idx);
 
+	ptr = buf = xmalloc(buf_sz);
 	/* string width */
 	if (vert) {
 		width = cf->menu->width + MENU_V_SPADDING;
@@ -1063,7 +1065,7 @@ static void ui_draw_menuitem(struct cfdisk *cf,
 		width = MENU_H_SPADDING + cf->menu->width + MENU_H_SPADDING;
 
 	name = _(d->name);
-	mbsalign(name, ptr, sizeof(buf), &width,
+	mbsalign(name, ptr, buf_sz, &width,
 			vert ? MBS_ALIGN_LEFT : MBS_ALIGN_CENTER,
 			0);
 
@@ -1082,6 +1084,7 @@ static void ui_draw_menuitem(struct cfdisk *cf,
 		mvprintw(ln, cl, "%s", buf);
 	else
 		mvprintw(ln, cl, "%s%s%s", MENU_H_PRESTR, buf, MENU_H_POSTSTR);
+	free(buf);
 
 	if (cf->menu->idx == idx) {
 		standend();
-- 
2.11.1


  reply	other threads:[~2017-02-13 22:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-13 22:06 [PATCH 0/7] pull: make smatch scan output easy to digest Sami Kerola
2017-02-13 22:06 ` Sami Kerola [this message]
2017-02-13 22:06 ` [PATCH 1/7] cfdisk: avoid use of VLA in combination with sizeof() [smatch scan] Sami Kerola
2017-02-13 22:06 ` [PATCH 3/7] misc: add static keyword to where needed " Sami Kerola
2017-02-13 22:06 ` [PATCH 4/7] misc: unify function declarations " Sami Kerola
2017-02-13 22:06 ` [PATCH 5/7] lib/idcache: add void to function declaration " Sami Kerola
2017-02-13 22:06 ` [PATCH 6/7] libblkid: declare across file variables in blkidP.h " Sami Kerola
2017-02-13 22:06 ` [PATCH 7/7] findmnt: fix couple memory leaks [cppcheck] Sami Kerola
2017-02-20 12:14 ` [PATCH 0/7] pull: make smatch scan output easy to digest Karel Zak
2017-02-20 22:12   ` Sami Kerola
2017-02-21  9:50     ` Karel Zak
2017-02-24  0:37       ` Dan Carpenter
2017-02-25 17:32         ` Sami Kerola
2017-02-25 20:09           ` Dan Carpenter
2017-02-26  4:07           ` Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170213220641.1395-2-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox