diff options
author | Michael Brown <mcb30@ipxe.org> | 2021-09-10 15:34:57 +0100 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2021-09-10 15:51:14 +0100 |
commit | 2265a65191d76ce367913a61c97752ab88ab1a59 (patch) | |
tree | 2f58659887657854975a751e4cf31dd7b0af064a | |
parent | 05a76acc6dc68bd962c12436ac9f48de1288fad2 (diff) | |
download | ipxe-2265a65191d76ce367913a61c97752ab88ab1a59.tar.gz |
[readline] Extend maximum read line length to 1024 characters
Realistic Linux kernel command lines may exceed our current 256
character limit for interactively edited commands or settings.
Switch from stack allocation to heap allocation, and increase the
limit to 1024 characters.
Requested-by: Matteo Guglielmi <Matteo.Guglielmi@dalco.ch>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r-- | src/hci/readline.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/hci/readline.c b/src/hci/readline.c index 852c4503a..ecc72d43f 100644 --- a/src/hci/readline.c +++ b/src/hci/readline.c @@ -38,7 +38,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * */ -#define READLINE_MAX 256 +#define READLINE_MAX 1024 /** * Synchronise console with edited string @@ -258,8 +258,8 @@ void history_free ( struct readline_history *history ) { int readline_history ( const char *prompt, const char *prefill, struct readline_history *history, unsigned long timeout, char **line ) { - char buf[READLINE_MAX]; struct edit_string string; + char *buf; int key; int move_by; const char *new_string; @@ -275,10 +275,14 @@ int readline_history ( const char *prompt, const char *prefill, /* Ensure cursor is visible */ printf ( "\033[?25h" ); - /* Initialise editable string */ + /* Allocate buffer and initialise editable string */ + buf = zalloc ( READLINE_MAX ); + if ( ! buf ) { + rc = -ENOMEM; + goto done; + } memset ( &string, 0, sizeof ( string ) ); - init_editstring ( &string, buf, sizeof ( buf ) ); - buf[0] = '\0'; + init_editstring ( &string, buf, READLINE_MAX ); /* Prefill string, if applicable */ if ( prefill ) { @@ -303,8 +307,13 @@ int readline_history ( const char *prompt, const char *prefill, switch ( key ) { case CR: case LF: - *line = strdup ( buf ); - rc = ( ( *line ) ? 0 : -ENOMEM ); + /* Shrink string (ignoring failures) */ + *line = realloc ( buf, + ( strlen ( buf ) + 1 /* NUL */ ) ); + if ( ! *line ) + *line = buf; + buf = NULL; + rc = 0; goto done; case CTRL_C: rc = -ECANCELED; @@ -332,6 +341,7 @@ int readline_history ( const char *prompt, const char *prefill, done: putchar ( '\n' ); + free ( buf ); if ( history ) { if ( *line && (*line)[0] ) history_append ( history, *line ); |