Skip to content

Commit 5ea9f64

Browse files
crquanmichal42
authored andcommitted
scripts/kconfig/nconf: dynamically alloc dialog_input_result
To support unlimited length string config items; No check for realloc return value keeps code simple, and to be consistent with other existing unchecked malloc in kconfig. Signed-off-by: Cheng Renquan <crquan@gmail.com> Signed-off-by: Arnaud Lacombe <lacombar@gmail.com>
1 parent cd58a90 commit 5ea9f64

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

‎scripts/kconfig/nconf.c‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ static int global_exit;
278278
/* the currently selected button */
279279
const char *current_instructions = menu_instructions;
280280

281+
static char *dialog_input_result;
282+
static int dialog_input_result_len;
283+
281284
static void conf(struct menu *menu);
282285
static void conf_choice(struct menu *menu);
283286
static void conf_string(struct menu *menu);
@@ -693,15 +696,14 @@ static void search_conf(void)
693696
{
694697
struct symbol **sym_arr;
695698
struct gstr res;
696-
char dialog_input_result[100];
697699
char *dialog_input;
698700
int dres;
699701
again:
700702
dres = dialog_inputbox(main_window,
701703
_("Search Configuration Parameter"),
702704
_("Enter " CONFIG_ " (sub)string to search for "
703705
"(with or without \"" CONFIG_ "\")"),
704-
"", dialog_input_result, 99);
706+
"", &dialog_input_result, &dialog_input_result_len);
705707
switch (dres) {
706708
case 0:
707709
break;
@@ -1346,7 +1348,6 @@ static void conf_choice(struct menu *menu)
13461348
static void conf_string(struct menu *menu)
13471349
{
13481350
const char *prompt = menu_get_prompt(menu);
1349-
char dialog_input_result[256];
13501351

13511352
while (1) {
13521353
int res;
@@ -1369,8 +1370,8 @@ static void conf_string(struct menu *menu)
13691370
prompt ? _(prompt) : _("Main Menu"),
13701371
heading,
13711372
sym_get_string_value(menu->sym),
1372-
dialog_input_result,
1373-
sizeof(dialog_input_result));
1373+
&dialog_input_result,
1374+
&dialog_input_result_len);
13741375
switch (res) {
13751376
case 0:
13761377
if (sym_set_string_value(menu->sym,
@@ -1390,14 +1391,13 @@ static void conf_string(struct menu *menu)
13901391

13911392
static void conf_load(void)
13921393
{
1393-
char dialog_input_result[256];
13941394
while (1) {
13951395
int res;
13961396
res = dialog_inputbox(main_window,
13971397
NULL, load_config_text,
13981398
filename,
1399-
dialog_input_result,
1400-
sizeof(dialog_input_result));
1399+
&dialog_input_result,
1400+
&dialog_input_result_len);
14011401
switch (res) {
14021402
case 0:
14031403
if (!dialog_input_result[0])
@@ -1422,14 +1422,13 @@ static void conf_load(void)
14221422

14231423
static void conf_save(void)
14241424
{
1425-
char dialog_input_result[256];
14261425
while (1) {
14271426
int res;
14281427
res = dialog_inputbox(main_window,
14291428
NULL, save_config_text,
14301429
filename,
1431-
dialog_input_result,
1432-
sizeof(dialog_input_result));
1430+
&dialog_input_result,
1431+
&dialog_input_result_len);
14331432
switch (res) {
14341433
case 0:
14351434
if (!dialog_input_result[0])

‎scripts/kconfig/nconf.gui.c‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...)
356356

357357
int dialog_inputbox(WINDOW *main_window,
358358
const char *title, const char *prompt,
359-
const char *init, char *result, int result_len)
359+
const char *init, char **resultp, int *result_len)
360360
{
361361
int prompt_lines = 0;
362362
int prompt_width = 0;
@@ -367,7 +367,12 @@ int dialog_inputbox(WINDOW *main_window,
367367
int i, x, y;
368368
int res = -1;
369369
int cursor_position = strlen(init);
370+
char *result = *resultp;
370371

372+
if (strlen(init)+1 > *result_len) {
373+
*result_len = strlen(init)+1;
374+
*resultp = result = realloc(result, *result_len);
375+
}
371376

372377
/* find the widest line of msg: */
373378
prompt_lines = get_line_no(prompt);
@@ -384,7 +389,7 @@ int dialog_inputbox(WINDOW *main_window,
384389
y = (LINES-(prompt_lines+4))/2;
385390
x = (COLS-(prompt_width+4))/2;
386391

387-
strncpy(result, init, result_len);
392+
strncpy(result, init, *result_len);
388393

389394
/* create the windows */
390395
win = newwin(prompt_lines+6, prompt_width+7, y, x);
@@ -443,7 +448,7 @@ int dialog_inputbox(WINDOW *main_window,
443448
case KEY_UP:
444449
case KEY_RIGHT:
445450
if (cursor_position < len &&
446-
cursor_position < min(result_len, prompt_width))
451+
cursor_position < min(*result_len, prompt_width))
447452
cursor_position++;
448453
break;
449454
case KEY_DOWN:
@@ -452,8 +457,13 @@ int dialog_inputbox(WINDOW *main_window,
452457
cursor_position--;
453458
break;
454459
default:
455-
if ((isgraph(res) || isspace(res)) &&
456-
len-2 < result_len) {
460+
if ((isgraph(res) || isspace(res))) {
461+
/* one for new char, one for '\0' */
462+
if (len+2 > *result_len) {
463+
*result_len = len+2;
464+
*resultp = result = realloc(result,
465+
*result_len);
466+
}
457467
/* insert the char at the proper position */
458468
memmove(&result[cursor_position+1],
459469
&result[cursor_position],

‎scripts/kconfig/nconf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void fill_window(WINDOW *win, const char *text);
8989
int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...);
9090
int dialog_inputbox(WINDOW *main_window,
9191
const char *title, const char *prompt,
92-
const char *init, char *result, int result_len);
92+
const char *init, char **resultp, int *result_len);
9393
void refresh_all_windows(WINDOW *main_window);
9494
void show_scroll_win(WINDOW *main_window,
9595
const char *title,

0 commit comments

Comments
 (0)