Skip to contentSkip to conversation

lxdialog: menubox: Add malloc allocation judgment

PATCH In review Li zeming 2 participants 3 messages Oct 24, 2022

Subsystems (2)
lore
Share & tools
lore

Li zeming

author
#0 lore raw

PATCH 1/1 lxdialog: menubox: Add malloc allocation judgment

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

---
 scripts/kconfig/lxdialog/menubox.c | 53 +++++++++++++++---------------
 1 file changed, 27 insertions(+), 26 deletions(-)
scripts/kconfig/lxdialog/menubox.c +27 -26
index 58c2f8afe59b..2ae263ad477e 100644
@@ -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);
 }
 
Signature · 1 line
2.18.2
#1 lore raw
Quoted 3 lines · Li zeming

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?

Signature · 1 line
An old man doll... just what I always wanted! - Clara
author
#2 lore raw

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.