Linux kbuild/kconfig development
 help / color / mirror / Atom feed
* [PATCH] lxdialog: menubox: Add malloc allocation judgment
@ 2022-10-24  5:56 Li zeming
  2022-10-24  8:11 ` Bagas Sanjaya
  0 siblings, 1 reply; 3+ messages in thread
From: Li zeming @ 2022-10-24  5:56 UTC (permalink / raw)
  To: masahiroy; +Cc: linux-kbuild, linux-kernel, Li zeming

Add menu_item Only when the pointer judges that the pointer is valid
can function code be executed.

Signed-off-by: Li zeming <zeming@nfschina.com>
---
 scripts/kconfig/lxdialog/menubox.c | 53 +++++++++++++++---------------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/scripts/kconfig/lxdialog/menubox.c b/scripts/kconfig/lxdialog/menubox.c
index 58c2f8afe59b..2ae263ad477e 100644
--- a/scripts/kconfig/lxdialog/menubox.c
+++ b/scripts/kconfig/lxdialog/menubox.c
@@ -55,34 +55,35 @@ static void do_print_item(WINDOW * win, const char *item, int line_y,
 {
 	int j;
 	char *menu_item = malloc(menu_width + 1);
+	if (menu_item) {
+		strncpy(menu_item, item, menu_width - item_x);
+		menu_item[menu_width - item_x] = '\0';
+		j = first_alpha(menu_item, "YyNnMmHh");
 
-	strncpy(menu_item, item, menu_width - item_x);
-	menu_item[menu_width - item_x] = '\0';
-	j = first_alpha(menu_item, "YyNnMmHh");
-
-	/* Clear 'residue' of last item */
-	wattrset(win, dlg.menubox.atr);
-	wmove(win, line_y, 0);
-#if OLD_NCURSES
-	{
-		int i;
-		for (i = 0; i < menu_width; i++)
-			waddch(win, ' ');
-	}
-#else
-	wclrtoeol(win);
-#endif
-	wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
-	mvwaddstr(win, line_y, item_x, menu_item);
-	if (hotkey) {
-		wattrset(win, selected ? dlg.tag_key_selected.atr
-			 : dlg.tag_key.atr);
-		mvwaddch(win, line_y, item_x + j, menu_item[j]);
-	}
-	if (selected) {
-		wmove(win, line_y, item_x + 1);
+		/* Clear 'residue' of last item */
+		wattrset(win, dlg.menubox.atr);
+		wmove(win, line_y, 0);
+	#if OLD_NCURSES
+		{
+			int i;
+			for (i = 0; i < menu_width; i++)
+				waddch(win, ' ');
+		}
+	#else
+		wclrtoeol(win);
+	#endif
+		wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
+		mvwaddstr(win, line_y, item_x, menu_item);
+		if (hotkey) {
+			wattrset(win, selected ? dlg.tag_key_selected.atr
+				 : dlg.tag_key.atr);
+			mvwaddch(win, line_y, item_x + j, menu_item[j]);
+		}
+		if (selected) {
+			wmove(win, line_y, item_x + 1);
+		}
+		free(menu_item);
 	}
-	free(menu_item);
 	wrefresh(win);
 }
 
-- 
2.18.2


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

* Re: [PATCH] lxdialog: menubox: Add malloc allocation judgment
  2022-10-24  5:56 [PATCH] lxdialog: menubox: Add malloc allocation judgment Li zeming
@ 2022-10-24  8:11 ` Bagas Sanjaya
  2022-10-24  9:39   ` Li zeming
  0 siblings, 1 reply; 3+ messages in thread
From: Bagas Sanjaya @ 2022-10-24  8:11 UTC (permalink / raw)
  To: Li zeming; +Cc: masahiroy, linux-kbuild, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 328 bytes --]

On Mon, Oct 24, 2022 at 01:56:29PM +0800, Li zeming wrote:
> Add menu_item Only when the pointer judges that the pointer is valid
> can function code be executed.
> 

The if block isn't "judgement", but conditional.

What if menu_item fails to be allocated?

-- 
An old man doll... just what I always wanted! - Clara

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH] lxdialog: menubox: Add malloc allocation judgment
  2022-10-24  8:11 ` Bagas Sanjaya
@ 2022-10-24  9:39   ` Li zeming
  0 siblings, 0 replies; 3+ messages in thread
From: Li zeming @ 2022-10-24  9:39 UTC (permalink / raw)
  To: bagasdotme; +Cc: linux-kbuild, linux-kernel, masahiroy, zeming


If malloc allocation fails, the menu_item pointer cannot be executed, because when the strncpy function is executed, there will be a problem with the null pointer. Adding an if statement should ensure that the allocation fails and will not be executed.

thanks.


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

end of thread, other threads:[~2022-10-24  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-24  5:56 [PATCH] lxdialog: menubox: Add malloc allocation judgment Li zeming
2022-10-24  8:11 ` Bagas Sanjaya
2022-10-24  9:39   ` Li zeming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox