diff mbox

[3/5] scripts/kconfig/nconf: dynamically alloc dialog_input_result

Message ID CACqU3MWiLBFBcm=bhGy8fJdZHT09khKC7u77HEwsY7QaSJ-RfQ@mail.gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Arnaud Lacombe Aug. 30, 2011, 4:26 a.m. UTC
Hi,

On Tue, Aug 30, 2011 at 12:14 AM, Arnaud Lacombe <lacombar@gmail.com> wrote:
> Hi,
>
> On Mon, Aug 29, 2011 at 7:56 PM, Cheng Renquan <crquan@gmail.com> wrote:
>>  int dialog_inputbox(WINDOW *main_window,
>>                const char *title, const char *prompt,
>> -               const char *init, char *result, int result_len)
>> +               const char *init, char *result, int *result_len)
>>  {
> This all logic will not work, you need to have the following prototype:
>
> int dialog_inputbox(WINDOW *main_window,
>                const char *title, const char *prompt,
>                const char *init, char **result, int *result_len)
>
> as realloc(3) will give you a new pointer.
>
Attached diff should do the job, this also avoid to have to deal with
`dialog_input_result' initialization.

 - Arnaud

>  - Arnaud
>
diff mbox

Patch

diff --git a/scripts/kconfig/nconf.c b/scripts/kconfig/nconf.c
index 18bfb34..53251f0 100644
--- a/scripts/kconfig/nconf.c
+++ b/scripts/kconfig/nconf.c
@@ -717,7 +717,7 @@  static void search_conf(void)
 			_("Search Configuration Parameter"),
 			_("Enter " CONFIG_ " (sub)string to search for "
 				"(with or without \"" CONFIG_ "\")"),
-			"", dialog_input_result, &dialog_input_result_len);
+			"", &dialog_input_result, &dialog_input_result_len);
 	switch (dres) {
 	case 0:
 		break;
@@ -1384,7 +1384,7 @@  static void conf_string(struct menu *menu)
 				prompt ? _(prompt) : _("Main Menu"),
 				heading,
 				sym_get_string_value(menu->sym),
-				dialog_input_result,
+				&dialog_input_result,
 				&dialog_input_result_len);
 		switch (res) {
 		case 0:
@@ -1410,7 +1410,7 @@  static void conf_load(void)
 		res = dialog_inputbox(main_window,
 				NULL, load_config_text,
 				filename,
-				dialog_input_result,
+				&dialog_input_result,
 				&dialog_input_result_len);
 		switch (res) {
 		case 0:
@@ -1441,7 +1441,7 @@  static void conf_save(void)
 		res = dialog_inputbox(main_window,
 				NULL, save_config_text,
 				filename,
-				dialog_input_result,
+				&dialog_input_result,
 				&dialog_input_result_len);
 		switch (res) {
 		case 0:
@@ -1508,15 +1508,6 @@  int main(int ac, char **av)
 			single_menu_mode = 1;
 	}
 
-	/* initially alloc 2048 bytes for dialog_input_result */
-	dialog_input_result_len = 2048;
-	dialog_input_result = malloc(dialog_input_result_len);
-	if (!dialog_input_result) {
-		fprintf(stderr, "Not enough memory for dialog_input_result(%d)\n",
-			dialog_input_result_len);
-		exit(1);
-	}
-
 	/* Initialize curses */
 	initscr();
 	/* set color theme */
diff --git a/scripts/kconfig/nconf.gui.c b/scripts/kconfig/nconf.gui.c
index 909c85f..8ae8d62 100644
--- a/scripts/kconfig/nconf.gui.c
+++ b/scripts/kconfig/nconf.gui.c
@@ -356,7 +356,7 @@  int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...)
 
 int dialog_inputbox(WINDOW *main_window,
 		const char *title, const char *prompt,
-		const char *init, char *result, int *result_len)
+		const char *init, char **resultp, int *result_len)
 {
 	int prompt_lines = 0;
 	int prompt_width = 0;
@@ -368,16 +368,16 @@  int dialog_inputbox(WINDOW *main_window,
 	int res = -1;
 	int cursor_position = strlen(init);
 	int cursor_form_win;
+	char *result = *resultp;
 
-	if (strlen(init) > *result_len) {
-		do {
-			*result_len *= 2;
-		} while (strlen(init) > *result_len);
+	if (strlen(init) + 1 > *result_len) {
+		*result_len = strlen(init) + 1;
 		result = realloc(result, *result_len);
-		/* here didn't check result, if it's NULL,
-		   just let it silently fail (SegFault) */
+		*resultp = result;
 	}
 
@@ -480,11 +480,9 @@  int dialog_inputbox(WINDOW *main_window,
 			if ((isgraph(res) || isspace(res))) {
 				/* one for new char, one for '\0' */
 				if (len+2 > *result_len) {
-					do {
-						*result_len *= 2;
-					} while (len+2 > *result_len);
+					*result_len = len+2;
 					result = realloc(result, *result_len);
-					/* silently fail in the same way above */
+					*resultp = result;
 				}
 				/* insert the char at the proper position */
 				memmove(&result[cursor_position+1],
diff --git a/scripts/kconfig/nconf.h b/scripts/kconfig/nconf.h
index 98b2c23..0d52617 100644
--- a/scripts/kconfig/nconf.h
+++ b/scripts/kconfig/nconf.h
@@ -89,7 +89,7 @@  void fill_window(WINDOW *win, const char *text);
 int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...);
 int dialog_inputbox(WINDOW *main_window,
 		const char *title, const char *prompt,
-		const char *init, char *result, int *result_len);
+		const char *init, char **resultp, int *result_len);
 void refresh_all_windows(WINDOW *main_window);
 void show_scroll_win(WINDOW *main_window,
 		const char *title,