From 8445642567fe20252e7c2cd6d0e994c952347a43 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 18 Apr 2023 06:37:26 -0300 Subject: [PATCH] cli: fix a panic and an infinite loop, when command flag descriptions have multiple lines (#17981) --- vlib/cli/cli_test.v | 25 +++++++++++++++++++++++++ vlib/cli/help.v | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 vlib/cli/cli_test.v diff --git a/vlib/cli/cli_test.v b/vlib/cli/cli_test.v new file mode 100644 index 00000000000000..10f8c9aa86c0dd --- /dev/null +++ b/vlib/cli/cli_test.v @@ -0,0 +1,25 @@ +module main + +import cli { Command, Flag } + +fn test_long_description() { + mut cmd := Command{ + name: 'cli' + description: 'An example of the cli library.' + version: '1.0.0' + } + mut greet_cmd := Command{ + name: 'greet' + description: 'Prints greeting in different languages.' + usage: '' + required_args: 1 + } + greet_cmd.add_flag(Flag{ + flag: .string_array + name: 'fun' + description: '\'{"uri":"mqtt://broker.emqx.io:1883","topic":"test_emq/1","filters":[{"producer_id":0,"trace_group":2,"string_id":3001},{"string_id":3002}]}\'' + }) + cmd.add_command(greet_cmd) + cmd.setup() + cmd.parse(['cli', 'greet', '-help']) +} diff --git a/vlib/cli/help.v b/vlib/cli/help.v index a41aa7891aadb2..8f62a5fe9b8510 100644 --- a/vlib/cli/help.v +++ b/vlib/cli/help.v @@ -153,8 +153,8 @@ fn pretty_description(s string, indent_len int) string { mut i := chars_per_line - 2 mut j := 0 for ; i < line.len; i += chars_per_line - 2 { - for line[i] != ` ` { - i-- + for j > 0 && line[j] != ` ` { + j-- } // indent was already done the first iteration if j != 0 {