mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 09/10] menu: sanitize menu_add_title
Date: Wed, 23 Nov 2016 10:39:36 +0100	[thread overview]
Message-ID: <20161123093937.15711-9-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20161123093937.15711-1-s.hauer@pengutronix.de>

passing a string to menu_add_title() which then gets free by this
function is just plain ugly. Pass it a const char * which gets
duplicated in menu_add_title() when necessary.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/menu.c |  2 +-
 common/boot.c   |  2 +-
 common/menu.c   | 12 +++++-------
 include/menu.h  |  2 +-
 4 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/commands/menu.c b/commands/menu.c
index 72db26e..ddc9c3a 100644
--- a/commands/menu.c
+++ b/commands/menu.c
@@ -147,7 +147,7 @@ static int do_menu_add(struct cmd_menu *cm)
 	if (!m->name)
 		goto free;
 
-	menu_add_title(m, strdup(cm->description));
+	menu_add_title(m, cm->description);
 
 	ret = menu_add(m);
 
diff --git a/common/boot.c b/common/boot.c
index 123b874..4306319 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -44,7 +44,7 @@ struct bootentries *bootentries_alloc(void)
 
 	if (IS_ENABLED(CONFIG_MENU)) {
 		bootentries->menu = menu_alloc();
-		menu_add_title(bootentries->menu, basprintf("boot"));
+		menu_add_title(bootentries->menu, "boot");
 	}
 
 	return bootentries;
diff --git a/common/menu.c b/common/menu.c
index 64df458..e757216 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -526,18 +526,17 @@ EXPORT_SYMBOL(menu_add_command_entry);
  * @display: NULL or pointer to the string which will be freed in this function.
  *	If NULL or zero length string is provided, default title will be added.
  */
-void menu_add_title(struct menu *m, char *display)
+void menu_add_title(struct menu *m, const char *display)
 {
 	char *tmp, *src, *dst;
 	int lines = 1;
 	int i;
 
-	if (!display || !strlen(display)) {
-		free(display);
-		display = xasprintf("Menu : %s", m->name ? m->name : "");
-	}
+	if (!display || !strlen(display))
+		src = dst = tmp = xasprintf("Menu : %s", m->name ? m->name : "");
+	else
+		src = dst = tmp = xstrdup(display);
 
-	src = dst = tmp = xstrdup(display);
 	/* Count lines and separate single string into multiple strings */
 	while (*src) {
 		if (*src == '\\') {
@@ -575,6 +574,5 @@ void menu_add_title(struct menu *m, char *display)
 	}
 
 	free(tmp);
-	free(display);
 }
 EXPORT_SYMBOL(menu_add_title);
diff --git a/include/menu.h b/include/menu.h
index 3e704a8..2fef064 100644
--- a/include/menu.h
+++ b/include/menu.h
@@ -91,7 +91,7 @@ int menu_set_selected_entry(struct menu *m, struct menu_entry* me);
 int menu_set_selected(struct menu *m, int num);
 int menu_set_auto_select(struct menu *m, int delay);
 struct menu* menu_get_menus(void);
-void menu_add_title(struct menu *m, char *display);
+void menu_add_title(struct menu *m, const char *display);
 
 /*
  * menu entry functions
-- 
2.10.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-11-23  9:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-23  9:39 [PATCH 01/10] ARM: socfpga: fix compiler warning Sascha Hauer
2016-11-23  9:39 ` [PATCH 02/10] nios2: Fix out of tree build Sascha Hauer
2016-11-23  9:39 ` [PATCH 03/10] nios2: Fix compiler warning Sascha Hauer
2016-11-23  9:39 ` [PATCH 04/10] globalvar: " Sascha Hauer
2016-11-23  9:39 ` [PATCH 05/10] mtd: peb: fix usage of uninitialized variable Sascha Hauer
2016-11-23  9:39 ` [PATCH 06/10] mtd: peb: Fix format specifier Sascha Hauer
2016-11-23  9:39 ` [PATCH 07/10] ARM: socfpga: fix compiler warning Sascha Hauer
2016-11-23  9:39 ` [PATCH 08/10] of: Use %pa to print resource_size_t type Sascha Hauer
2016-11-23  9:39 ` Sascha Hauer [this message]
2016-11-23  9:39 ` [PATCH 10/10] arm: at91 bootstrap: Fix menu title Sascha Hauer

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=20161123093937.15711-9-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.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