From patchwork Tue Aug 30 04:26:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Arnaud Lacombe X-Patchwork-Id: 1111912 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.4) with ESMTP id p7U4QGaH025116 for ; Tue, 30 Aug 2011 04:26:16 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752249Ab1H3E0P (ORCPT ); Tue, 30 Aug 2011 00:26:15 -0400 Received: from mail-pz0-f42.google.com ([209.85.210.42]:52736 "EHLO mail-pz0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752222Ab1H3E0P (ORCPT ); Tue, 30 Aug 2011 00:26:15 -0400 Received: by pzk37 with SMTP id 37so9624538pzk.1 for ; Mon, 29 Aug 2011 21:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=7kahOkZlXVk2Pxneor9sPG4UZW4GRP8Ac8I1V4A9lNA=; b=tBin28gsHqwCnpNiBGGP5Yr0uTI9EgPB0nIo1MtlTuQTLhVSzKuqSgYgg3uVRLJeq+ Ze2PAcReA8g0q2Qsd2fLnTYda00E0A3yID/+laUde1TlFmgNfFbkIomPYwwvnRjG5iBj 2iaIyaaaj2EhEhMMh2uDE4f0d5INr1vhlxPgk= MIME-Version: 1.0 Received: by 10.142.194.3 with SMTP id r3mr3063834wff.291.1314678374826; Mon, 29 Aug 2011 21:26:14 -0700 (PDT) Received: by 10.142.105.2 with HTTP; Mon, 29 Aug 2011 21:26:14 -0700 (PDT) In-Reply-To: References: <1314662164-8565-1-git-send-email-crquan@gmail.com> <1314662164-8565-2-git-send-email-crquan@gmail.com> <1314662164-8565-3-git-send-email-crquan@gmail.com> Date: Tue, 30 Aug 2011 00:26:14 -0400 Message-ID: Subject: Re: [PATCH 3/5] scripts/kconfig/nconf: dynamically alloc dialog_input_result From: Arnaud Lacombe To: Cheng Renquan Cc: linux-kbuild@vger.kernel.org, Sam Ravnborg , Michal Marek , Nir Tzachar , Randy Dunlap , c.rq541@comcast.net Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Tue, 30 Aug 2011 04:26:16 +0000 (UTC) Hi, On Tue, Aug 30, 2011 at 12:14 AM, Arnaud Lacombe wrote: > Hi, > > On Mon, Aug 29, 2011 at 7:56 PM, Cheng Renquan 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 --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,